Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why std::is_same<int, *(int*)>::value is false? [duplicate]

Tags:

c++

c++11

is-same

Should *(int*) be just int in C++11?

I run compile below code with g++ -std=c++11, it outputs "0", which is out of my expectation.

int num = 0;                       
int *num_ptr = &num;               
cout<<std::is_same<decltype(num), decltype(*num_ptr)>::value<<endl;//0
like image 692
ppzzyy Avatar asked Feb 21 '26 18:02

ppzzyy


1 Answers

Besides types, decltype also considers value categories.

For id-expression, i.e. num, decltype leads to its type which is int.

For *num_ptr, which is an lvalue-expression, decltype leads to T& i.e. int&.

  1. If the argument is an unparenthesized id-expression or an unparenthesized class member access expression, then decltype yields the type of the entity named by this expression.

  2. If the argument is any other expression of type T, and

    a) if the value category of expression is xvalue, then decltype yields T&&;

    b) if the value category of expression is lvalue, then decltype yields T&;

    c) if the value category of expression is prvalue, then decltype yields T.

like image 106
songyuanyao Avatar answered Feb 24 '26 09:02

songyuanyao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!