Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template function type deduction, and return type

Why a is true, and b is false? Or in other words why T in foo1 is int const but return type of foo2 is just 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;
}
like image 746
omicronns Avatar asked Jan 29 '23 21:01

omicronns


1 Answers

The specialization called, const int foo2<const int>(const int&);, has a return type of const int, so foo2(x) would have been a prvalue of type const int. However, There are no const (or volatile) prvalues of non-array, non-class type (in your case, int). The constness is adjusted away "prior to any further analysis", and it becomes simply a prvalue of type int, which decltype reports.

like image 186
T.C. Avatar answered Feb 04 '23 08:02

T.C.