【发布时间】:2013-02-26 13:22:55
【问题描述】:
我有这样的层次结构:
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
【问题讨论】:
-
g++ 4.7.2 的行为符合预期:ideone.com/8yrx6k
-
Clang 4.0 的行为符合预期。
-
请下次提供重现问题的可编译代码。只需编译您的代码,您就会发现它在语法上不正确。
-
你的类型是否完整?
-
仍然有效:ideone.com/RfDuPr