Consider the code below:
int main()
{
int i{};
auto& c = static_cast<const int&&>(i);
auto& v = static_cast<volatile int&&>(i);
}
While (1)
compiled successfully, (2)
it is not accepted:
error: volatile lvalue reference to type "volatile int" cannot be attached to temporary type "volatile int"
Why auto
can't it be volatile int
?
Why auto&
becomes const int
and becomes attached to const int&&
? Is it because it auto&
actually binds to a temporary object created on the right side of the destination? But then why auto& p = 1;
doesn't it work?
source
share