我想问你关于这段代码的一些建议。它有效,但我认为它可以用更优雅的方式编写。这是一段C ++ 11代码,所以在编译时请记住它;)!
#include <iostream>
#include <type_traits>
#include <typeinfo>
using namespace std;
class A {};
class B: public A {};
class C {};
class D: public C {};
class E: public A, public C {};
template<class T, typename = void>
class Example;
template<class T>
class Example<T, typename enable_if<is_base_of<A, T>::value and not is_base_of<C, T>::value>::type>
{
public:
string a() const
{
return string(typeid(T).name()) + " have A as base class";
}
};
template<class T>
class Example<T, typename enable_if<not is_base_of<A, T>::value and is_base_of<C, T>::value>::type>
{
public:
string b() const
{
return string(typeid(T).name()) + " have C as base class";
}
};
template<class T>
class Example<T, typename enable_if<is_base_of<A, T>::value and is_base_of<C, T>::value>::type> :
public Example<A>,
public Example<C>
{
};
int
main()
{
Example<B> example1;
Example<D> example2;
Example<E> example3;
cout << example1.a() << endl;
//cout << example1.b() << endl; It must raise a compile error
//cout << example2.a() << endl; It must raise a compile error
cout << example2.b() << endl;
cout << example3.a() << endl;
cout << example3.b() << endl;
}
正如您所看到的,我正在尝试编写一个类模板,该模板可以处理从 A 和 C 派生的类。问题是 A 和 C 是否继承了类 E 。事实上,我们也可能有类似的东西......
template<class T>
class Example<T, typename enable_if<is_base_of<A, T>::value> { /* ... */ };
template<class T>
class Example<T, typename enable_if<is_base_of<C, T>::value> { /* ... */ };
...但是当一个类(如 E )继承 A 和 C 时它会失败。
有关更好代码的任何想法吗? 感谢
答案 0 :(得分:4)
更简单的方法是使用static_assert
。
template <typename T>
class Example
{
public:
std::string a() const
{
static_assert(std::is_base_of<A, T>::value, "T must derive from A to use a()");
return std::string(typeid(T).name()) + " have A as base class";
}
std::string b() const
{
static_assert(std::is_base_of<C, T>::value, "T must derive from C to use b()");
return std::string(typeid(T).name()) + " have C as base class";
}
};