【发布时间】:2012-11-26 10:24:17
【问题描述】:
是否有可能静态断言作为模板参数提供的类型是否实现了参数包中列出的所有类型,即。参数包感知 std::is_base_of()?
template <typename Type, typename... Requirements>
class CommonBase
{
static_assert(is_base_of<Requirements..., Type>::value, "Invalid.");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
parameter pack aware version of std::is_base_of()
public:
template <typename T> T* as()
{
static_assert(std::is_base_of<Requirements..., T>::value, "Invalid.");
return reinterpret_cast<T*>(this);
}
};
【问题讨论】:
-
我怀疑这是否可能。您的第一个
is_base_of<Requirements...>::value也没有提到第二个参数。 -
static_assertion是编译时进程(here),编译时是否检查is_base_of的值? -
@tAmirNaghizadeh 当然可以,因为
is_base_of本身就是一个模板,模板在编译时被实例化。
标签: c++ templates variadic-templates typetraits