Why can't `auto &` bind to a volatile rvalue expression?

Consider the code below:

int main()
{
  int i{};
  auto& c = static_cast<const int&&>(i);    // (1)
  auto& v = static_cast<volatile int&&>(i); // (2)
}

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 autocan't it be volatile int?

Why auto&becomes const intand 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?

+4
source share
1 answer

auto, .

const int&, . , a volatile int& .

+7

Source: https://habr.com/ru/post/1611182/


All Articles