c++ - 模板函数类型推导和返回类型

标签 c++ templates template-argument-deduction

为什么 atrue,而 bfalse?或者换句话说,为什么 foo1 中的 Tint constfoo2 的返回类型只是 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;
}

最佳答案

专业称为,const int foo2<const int>(const int&); , 返回类型为 const int , 所以 foo2(x)本来是 const int 类型的纯右值.但是,没有 const (或 volatile )非数组、非类类型的纯右值(在您的情况下为 int )。 constness 被调整掉了 "prior to any further analysis" , 它变成了一个类型为 int 的纯右值,这decltype报告。

关于c++ - 模板函数类型推导和返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48137387/

相关文章:

c++ - 显式传递模板参数时,函数模板参数丢失常量?

c++ - C++ 中从非引用类型自动推导引用模板参数

c++ - 通过 Makefile 的预处理器宏定义在 C++ 中无法正常工作

c++ - 细粒度访问说明符 C++

c++ - 模板函数调用被函数混淆,在模板之前声明了错误的签名

c++ - 书中好像有矛盾 "C++ Templates - The Complete Guide"

c++ - 可以在 .hpp 文件中重载全局运算符吗?

c++ - boost::TIME_UTC(_) 具有不同的 boost 版本

c++ - 是否可以根据约束 "overload"别名模板?

c++ - 具有与实例一样多的参数的可变参数模板