说我有一些模板类:如何确定类型是否在编译时从模板类派生?
template<class T>
class Foo{}
template<class T>
class Bar{}
现在,我要确保(在编译时),在Bar
使用的类型从Foo
的。我已经找到this答案,说明如何在运行时做到这一点,但我想在编译时检查,也许使用static_assert
什么的。
有没有办法做到这一点?
说我有一些模板类:如何确定类型是否在编译时从模板类派生?
template<class T>
class Foo{}
template<class T>
class Bar{}
现在,我要确保(在编译时),在Bar
使用的类型从Foo
的。我已经找到this答案,说明如何在运行时做到这一点,但我想在编译时检查,也许使用static_assert
什么的。
有没有办法做到这一点?
现在,我想确保(在编译时)Bar中使用的类型来自Foo。
你可以做这样的事情:
#include<type_traits>
#include<utility>
template<class T>
class Foo{};
template<typename T>
std::true_type test(const Foo<T> &);
std::false_type test(...);
template<class T>
class Bar {
static_assert(decltype(test(std::declval<T>()))::value, "!");
};
struct S: Foo<int> {};
int main() {
Bar<S> ok1;
Bar<Foo<int>> ok2;
// Bar<int> ko;
}
看到它的wandbox。
其基本思想是,如果T
来自Foo
的专门化,则无论是U
,都可以将T
类型的临时文件绑定到const Foo<U> &
。因此,您可以声明(无需定义)一些函数(如示例中的函数)来测试它,然后在static_assert
或任何其他常量上下文中使用声明的返回类型。
编辑
正如意见建议的@Quentin,或许这是值得的指针代替引用防止误报的转换构造和运营商。
可能有一些[type-traits](http://en.cppreference.com/w/cpp/types#Type_traits_.28since_C.2B.2B11.29)可以使用。 –
@Someprogrammerdude是的,我研究了一下。 is_base_of可能工作,但我只知道如何使用它时,Foo是一个普通的类而不是模板类。 – Mastrem