decltype error C2440 cannot convert from 'int *' to 'int *&'

后端 未结 2 477
南方客
南方客 2021-01-18 17:08

The following is a contrived example of the actual code:

int** Ptr = 0;
decltype(Ptr[0]) Test = (int*)0;

I get the error:

2条回答
  •  隐瞒了意图╮
    2021-01-18 17:56

    In addition to all the other answers, you may also just use

    int ** ptr = 0;
    decltype(+ptr[0]) test = (int*)0;
    // (+*p) is now an r-value expression of type int, rather than int&
    

    Some of the rules used here are:

    • if the value category of expression is lvalue, then decltype yields T&;
    • if the value category of expression is prvalue, then decltype yields T.

    Also note that if the name of an object is parenthesized, it is treated as an ordinary lvalue expression, thus decltype(x) and decltype((x)) are often different types. [an excerpt from https://en.cppreference.com/w/cpp/language/decltype]

    seen at http://www.cplusplus.com/forum/beginner/149331/
    more on decltype here : https://en.cppreference.com/w/cpp/language/decltype

提交回复
热议问题