samedi 9 février 2019

Why is `std::remove_const` not removing the `const`-ness of a reference object when used with `decltype`?

#define T int
int main ()
{
  const T x = 2;
  // (1) -- type of `x` is compared with `T`
  static_assert(std::is_same<std::remove_const<decltype(x)>::type, T>::value, "Not same"); // fails
  // (2) -- type of `x` is compared with `const T`
  static_assert(std::is_same<std::remove_const<decltype(x)>::type, const T>::value, "Not same");  // ok
}

Above code works as expected. Where the (1) passes and the (2) fails.

However, it happens other way around, viz. (1) fails and (2) passes, if I make following change:

#define T int&

Why does it happen so?

With the similar code of decltype, what can we add to the code, so that (1) passes with reference & non-reference types both, i.e. int& & int?
Possible solution with const_cast are also welcome.


Note: Since I want to macro-fy the removal of const from an object; I have used decltype.

Aucun commentaire:

Enregistrer un commentaire