为什么 a
是 true
,而 b
是 false
?或者换句话说,为什么 foo1
中的 T
是 int 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;
}
最佳答案
专业称为,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/