Why isn't std::declval<int>() an lvalue when std::declval<int&>() is?

175 Views Asked by At

The first of these two lines fails to compile, the second compiles in MSVC 2017:

//    std::cout << sizeof(decltype(++std::declval<int>())) << "\n";  //error.  expression must be a modifiable lvalue.
    std::cout << sizeof(decltype(++std::declval<int&>())) << "\n";

The error message is "expression must be a modifiable lvalue."

I know that declval adds an rvalue reference. So in the first line, the compiler considers (does it not?) the outcome of std::declval<int>() to be an rvalue reference. In the second line, the compiler considers the outcome of std::declval<int&>() to be an lvalue reference, due to the collapsing rules.

Why is the rvalue reference not a modifiable lvalue, while the lvalue reference is?

0

There are 0 best solutions below