std::is_base_of() depth
std::is_base_of() depth
我有一个这样的层次结构:
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输出true
:
class I1{};
class I2: public I1{};
class I3: public I2{};
int main(int argc, const char* argv[])
{
std::cout << std::boolalpha
<< std::is_base_of<I1, I3>::value
<< std::endl;
return 0;
}
注意,std::is_base_of<I1, I3>::value
等价于实例化一个类型为std::is_base_of<I1, I3>
的对象并将其转换为bool
。
我相信这样做是准确的。std::is_base_of<Base, Derived>
被定义为具有条件(§20.9.6):
Base
是Derived
(10)的基类,而不考虑cv-限定符,或者Base
和Derived
不是并集,并且命名相同的类类型,而不考虑cv-限定符
基类定义如下(§10):
类
B
是类D
的基类,如果它是D
的直接基类或D
的一个基类的直接基类
所以是的,I1
是I3
的基类,std::is_base_of<I1, I3>::value
应该是true
。
不,代码可以工作:
#include <iostream>
#include <type_traits>
class A {};
class B : public A {};
class C : public B {};
int main() {
std::cout << std::boolalpha;
std::cout << "a2b: " << std::is_base_of<A, B>() << 'n';
std::cout << "b2a: " << std::is_base_of<B, A>() << 'n';
std::cout << "c2b: " << std::is_base_of<C, B>() << 'n';
std::cout << "a2c: " << std::is_base_of<A, C>() << 'n';
}
收益率:
a2b: true
b2a: false
c2b: false
a2c: true
上面的代码可以在GCC 4.6和4.7上运行。如果您使用其他内容,则需要指定。
我找到问题了。我有几个带有单元测试的.cpp文件,我在其中定义了具有相同名称的本地类。还没有在标准中找到,但是复制代码类似于:
/// 1.cpp
class IInterface{};
class Interface: public IInterface{};
/// class 2.cpp
class IInterface: public MyService:IBase{};
class Interface: public IInterface{};
/// facility.h
namespace a{ namespace b{
template<class T, class B>
bool IsBaseOf(){
return std::is_base_of<T, B>();
}
}
}
}
/// ...main
a::b::IsBaseOf<MyService::IBase, Interface>();
它应该/可以失败。但是如果我让类名唯一,那就没问题了
相关文章:
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 构造函数采用 Base&不被调用
- 将sharet_ptr<Derived>转换为shared_ptr<Base>
- 将 BASE 派生类存储在同一容器中
- 避免矢量中的对象切片<Base><shared_ptr>
- 从 std<Derived>::shared_ptr 的向量返回 std::shared_ptr<Base>
- 如何从unique_ptr返回unique_ptr的引用<Derived><Base>?
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 如何将unique_ptr<derived>*转换为<base>unique_ptr*?
- <Base> <Derived> 具有相同原始指针共享引用的 shared_ptr 和 shared_ptr 实例是否计数?
- 如何在C++中设置演员的"Render Custom Depth Pass"和"Depth Stencil Value"?
- 从 Base 引用对象调用派生类的成员
- 两个成员,在Base中默认初始化,可能在Derived中非默认初始化
- 从"<Base>std::unique_ptr"创建"ClassType<std::unique_ptr>"时出现编译错误<Derived>
- C++ - 打印派生类对象的矢量<base*> 元素
- 为什么在此示例中从unique_ptr自动向上转换<derived>到unique_ptr<base>失败?
- 如何在派生类中不显式调用base::func()的情况下从基类执行虚拟函数
- Visual Studio MFC C++ "CFormView" "base class"下拉列表中缺少
- 尝试应用肖恩父母谈话"inheritance is the base class of evil"中的模式
- 派生对象调用的 Base 方法的模板推导