编译时断言? [英] Compile-time assertion?

查看:122
本文介绍了编译时断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法,我可以断言两个常量表达式在编译时是相等的?

Is there a way I can assert that two constant expressions are equal at compile time?

例如。我希望这会导致编译时错误

e.g. I want this to cause a compile-time error

enum { foo=263, bar=264 };
SOME_EXPRESSION(foo,bar)

但我希望这不会导致错误



but I want this to not cause an error

enum { foo=263, bar=263 };
SOME_EXPRESSION(foo,bar)






编辑:上面被简化了。我的情况更像是


edit: the above was simplified. My situation is more like

some_other_file_I_dont_control.h:

some_other_file_I_dont_control.h:

class X
{
public:
   enum { foo=263 };
}

my_file.h:

my_file.h:

enum { bar=something+somethingelse }; // bar should equal X::foo
SOME_EXPRESSION(X::foo, bar)


推荐答案

请参阅 static_assert (仅限C ++ 0x);如果是旧版本,请参阅 Boost的 StaticAssert

这篇关于编译时断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆