mercredi 12 août 2020

Is converting decltype's expression from constant l-value into an r-value discards `const` too?

Here I want to know how type specifier decltype works:

const int& rci = 5;// const ref bound to a temporary
decltype (rci) x = 2;
decltype (rci + 0) y = 10;
// ++x; // error: increment of read-only reference ‘x’|
++y; // why does this work

std::cout << typeid(x).name() << std::endl; // i
std::cout << typeid(y).name() << std::endl; // i
  • Why y has only type int rather than const int?

  • Does converting a constant lvalue into an rvalue discards in addition to reference operator, const qualifier too?

  • Why typeid shows that the type of x is just i but not const int&?

Aucun commentaire:

Enregistrer un commentaire