Type function type inference and return type

Why ais there true, and b- false? Or, in other words, why Tis foo1there int const, but is the return type foo2equal int?

template<typename T>
constexpr bool foo1(T &) {
    return std::is_const<T>::value;
}

template<typename T>
T foo2(T &);

int main() {
    int const x = 0;
    constexpr bool a = foo1(x);
    constexpr bool b = std::is_const<decltype(foo2(x))>::value;
}
+4
source share
2 answers

const-qualifiers are ignored if the return type of the function is non-classical and not an array. If you use some class instead of plain int, it will produce 1 1:

  struct Bar{};

  int main()
  {
     Bar const x{};
     constexpr bool a = foo1(x);
     constexpr bool b = std::is_const<decltype(foo2(x))>::value;
  }

online compiler

+2
source

, const int foo2<const int>(const int&);, const int, foo2(x) const int. , const ( volatile) prvalues ​​ non-array, non-class ( int). " " , int, decltype.

+6

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