【发布时间】:2015-05-05 10:26:33
【问题描述】:
第一个问题:假设我有一个像这样的自定义struct:
struct Var {};
struct is_var_type: std::integral_constant<bool, std::is_same<Var, typename std::remove_cv<T>::type>::value> {};
在此之后,我想通过模板参数 SFINAE 使用std::enable_if 获得一些自定义类型,但是我无法弄清楚。我只能这样做:
template<typename T>
void variant(T a)
{
if (is_var_type<Var>::value){
std::cout << " " << true;
}
else {
std::cout << " " << false;
}
}
我想做这样的东西,但是std::is_integral 已经被定义了。如何定制?
template<class T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr> // I couldn't figure out how to make custom integral_constant via template parameter
T foo3(T t) // note, function signature is unmodified
{
return t; //so this type should be an int, but I want to make sure that this type T is is_var_type struct, is this possible?
}
第二个问题:如果我们添加一些这样的容器类型,这种方式可能吗?
struct Var {};
struct ListVar: public std::list<Var>
struct is_var_type: std::integral_constant<bool, std::is_same<ListVar, typename std::remove_cv<T>::type>::value> {};
– 但我遇到了一个更令人困惑的编译时错误,例如无法在gcc 4.9 中推断出多个类型。
【问题讨论】:
-
不应该
is_var_type是模板吗? -
我再次编辑了我的问题,以使其更清楚。
-
struct Var:{};- 为什么是冒号?这不会编译。
标签: list c++11 templates std enable-if