我有这样的层次结构:
namespace MyService{
class IBase
{
public:
virtual ~IBase(){}
protected:
IPointer *_somePointer;
};
}
class IInterface: public MyService::IBase
{
public:
virtual ~IInterface(){}
virtual std::string get() const = 0;
};
class ConcreteClass: public IInterface
{
public:
std::string get() const
{
bool isNull = (_somePointer == NULL);
return "Hello";
}
};
bool isBase = std::is_base_of<IBase, ConcreteClass>::value;
我需要检查 I3 是从 I1 派生的。但是 std::is_base_of() 对我来说效果不佳 - 它返回错误。目标是添加到任何类 IBase 并检查任何类在它的层次结构中是否有 IBase
找到了问题,但没有解决。我的代码是:
template<class Base, class Derived>
bool IsDerivedFromBase()
{
if( std::tr1::is_fundamental<Base>::value )
throw MyService::Exceptions::ETypeTraitsInvalidArgument( "Base class can't be POD" );
if( std::tr1::is_fundamental<Derived>::value )
throw MyService::Exceptions::ETypeTraitsInvalidArgument( "Derived class can't be POD" );
bool a = std::tr1::is_base_of<Base, Derived>::value;
return a;
}
我有一个这样的
bool a = std::is_base_of<MyService::IBase, SomeInterface>::value; // true
a = IsDerivedFromBase<MyService::IBase, SomeInterface>(); // false