如果class A
的模板具有特定的模板class NOTALLOWED
,如何抛出 static_assert ?
template<typename T>
struct NOTALLOWED{
};
template<typename T>
struct A{
// static_assert if T == NOTALLOWED<...> ??
}
// USING A< NOTALLOWED<int> > is not allowed for example
模板类A应保持原样。
我想阻止A
将结构NOTALLOWED
作为模板参数
非常感谢!
答案 0 :(得分:1)
您可以为特定模板编写is_instantiation
个特征:
template <typename T>
struct is_notallowed_instantiation { constexpr bool value = false; };
template <typename... Args>
struct is_notallowed_instantiation<NOTALLOWED<Args...>> { constexpr bool value = true; };
然后你可以static_assert
就可以了。
答案 1 :(得分:0)
专注于特殊模板:
template<typename T>
struct NOTALLOWED{
};
template<typename T>
struct A{
// normal code
}
template<typename T>
struct A< NOTALLOWED<T> >{
std::static_assert( sizeof(NOTALLOWED<T>) == -1, "This is not allowed!")
}