有没有办法防止使用静态断言和类型特征两次派生类?
我意识到这是一个人为的示例,但我想要进行编译检查以防止这种情况......
class A {};
class B : public A {};
class C : public A {};
class D : public B, public C
{
BOOST_STATIC_ASSERT((is_base_of_once<A,D>::value))
};
I realize this is a contrived example, but I want a compile check to prevent this...
class A {};
class B : public A {};
class C : public A {};
class D : public B, public C
{
BOOST_STATIC_ASSERT((is_base_of_once<A,D>::value))
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下应该有效:
如果 A 存在两次,这应该会引发歧义错误,否则测试将始终成功(因为它比较两个空指针)。
The following should work:
If A exists twice, this should rise an ambiguity error, while otherwise the test will always succeed (because it compares two null pointers).
当我尝试派生一个类两次时,它甚至无法编译。 (重复碱基类型)
When I try to derive a class twice as you have here it does not even compile. (duplicate base type)
如果您真的愿意,您可以测试两个基类:
否则您可以使这些类从 A 继承,这样派生类中仍然只有一个它的实例:
If you really want to, you an test both your base classes:
Otherwise you can make the classes inherit virtually from A, so that there will still only be one instance of it in the derived class: