c++ - std::is_same result with lvalue and rvalue reference -


i playing std::is_same utility function in combination rvalue , lvalue reference , came across weird behavior.

consider function template checks type of variable t.

i using vs 2013 :

struct test {};  template < class t> void h(t && t) {      cout << " type &&: " << std::is_same<decltype(t), t &&>::value << endl;     cout << " type &: " << std::is_same<decltype(t), t &>::value << endl; } 

i observe following outputs :

h(test()); // type && : 1  type & : 0 

which normal test() temporary object, , universal reference in parameter of h resolves r value reference (&& && = &&)

but consider :

test mytest; h(mytest); // type && : 1  type & : 1 !!! 

same result if write :

test &mytest = test(): h(mytest); // type && : 1  type & : 1 !!! 

and same :

test &&mytest = test(): h(mytest); // type && : 1  type & : 1 !!! 

am missing ? looks complete mess me :) features rvalue reference / decltype not supported in vs 2013?

thanks help

romain

romain, easy enough. let's condider following case:

test mytest; h(mytest); // type && : 1  type & : 1 !!! 

inside h, t test&, , t of type test& &&, is, test&. when test, std::is_same<decltype(t), t &&> true, since test& && test&, , t type test&.

std::is_same<decltype(t), t &> true, since t type test&, , t& test&&, test&.

careful application of reference collapsing rules helps here.

a bit on why inside h() t of type test&. reason is type of t match actual argument. t can not test, since test&& not bind(match) lvalue of type test (as argument type). however, test& && will, because of reference collapsing rules.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -