模板函数类型推导和返回类型

omi*_*nns 4 c++ templates template-argument-deduction

为什么atruebfalse?或者换句话说,为什么Tfoo1int const,但返回的类型foo2就是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;
}
Run Code Online (Sandbox Code Playgroud)

T.C*_*.C. 6

名为,,,的专业化const int foo2<const int>(const int&);具有返回类型const int,因此foo2(x)将是类型的prvalue const int.但是,没有const(或volatile)非数组,非类类型(在您的情况下)的prvalues int.在"进行任何进一步分析之前"调整常数,并且它变为简单的类型的prvalue int,其decltype报告.