我有一個這樣的層次結構:的std :: is_base_of()深度
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()對我來說效果不好 - 它返回false。 目標是添加到任何班級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表現如預期:http://ideone.com/8yrx6k – hmjd 2013-02-26 13:28:36
Clang 4.0的行爲如預期。 – 2013-02-26 13:29:55
請在下次提供可複製問題的代碼。只要編譯你的代碼,你會發現它的語法不正確。 – mfontanini 2013-02-26 13:32:59