【发布时间】:2019-12-22 15:44:20
【问题描述】:
我有一段漂亮的代码,它使用C++14s variable templates:
#include <typeinfo>
template<typename T, typename U>
constexpr bool same_type = false;
template<typename T>
constexpr bool same_type<T,T> = true;
int main() {
bool f = same_type<int, bool>; // compiles. Evals to false.
bool t = same_type<int, int>; // compiles. Evals to true.
int a;
int b;
return same_type<typeid(a), typeid(a)>; // does not compile.
}
它检查两种类型是否相同。我喜欢这个,但是如果我必须自己传递类型而不是从某些变量中派生它们,这对我来说似乎毫无用处。
有没有办法让它工作?我本来预计像 typeid(x) 这样的东西可能会成功。
【问题讨论】:
标签: c++ types c++14 typeid variable-templates