I am a little bit confused about why decltype
with the comma operator returns reference type in some cases.
for example, in this code:
int i = 101;
decltype(1, i) var = i;
var = 20;
printf("%d\n", i); // will print 20
here, var is int& instead of int, but if I replace the second line with:
decltype(i) var = i;
it will return int!
can anyone please explain that?
decltype
is special-cased for an unparenthesized id-expression to give the type of the entity, without reference qualification [dcl.type.simple]:
4 - The type denoted by
decltype(e)
is defined as follows:
— ife
is an unparenthesized id-expression or an unparenthesized class member access (5.2.5),decltype(e)
is the type of the entity named bye
. [...]
— otherwise, ife
is an lvalue,decltype(e)
isT&
, whereT
is the type ofe
; [...]
By providing a comma expression you are disabling this special case, as with parentheses:
decltype(i) // int
decltype((i)) // int&
decltype(1, i) // int&
(i)
and 1, i
are lvalue expressions, so their decltype
is a reference type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With