[英]Compile-time assertion?
有没有办法可以断言两个常量表达式在编译时相等?
例如,我希望这会导致编译时错误
enum { foo=263, bar=264 };
SOME_EXPRESSION(foo,bar)
但我希望这不会导致错误
enum { foo=263, bar=263 };
SOME_EXPRESSION(foo,bar)
编辑:以上已简化。 我的情况更像
some_other_file_I_dont_control.h:
class X
{
public:
enum { foo=263 };
}
我的文件.h:
enum { bar=something+somethingelse }; // bar should equal X::foo
SOME_EXPRESSION(X::foo, bar)
是的。 您可以使用 bool 类型的模板特化来做到这一点,如下所示:
// empty default template
template <bool b>
struct StaticAssert {};
// template specialized on true
template <>
struct StaticAssert<true>
{
static void assert() {}
};
int f()
{
StaticAssert<1==1>::assert(); // compiles fine, assert() member found
StaticAssert<1==2>::assert(); // compile failure, no assert() member for StaticAssert<false>
}
代码基本上来自 memory,可能需要一些调整。
参见static_assert
(仅限 C++0x); 如果在旧版本上,请参阅Boost 的StaticAssert
。
对于 static 断言的另一个版本,您可以通过添加更好的名称来美化,您可以使用:
// name must be a valid identifier
#define STATIC_ASSERT( condition, name )\
typedef char assert_failed_ ## name [ (condition) ? 1 : -1 ];
并用作:
STATIC_ASSERT( x == y, constants_must_be_same );
编译器将触发类似于以下的错误:
size of array 'assert_failed_constants_must_be_same' is negative
这似乎没有多大帮助,但它会指向断言的确切行,一段时间后您将开始处理该错误消息,因为static 断言失败
Windows 的另一种可能性是C_ASSERT ,如果包含 Windows.h 则定义。
还有使用switch (..)
语句的技巧。 不过有点老套路。 案例条目 foo == bar 必须在编译时进行评估,如果它碰巧为假,则 switch 语句将导致错误。 编译器还会将其减少为“无”。
{
bool x=false;
switch (x) {
case foo == bar:
break;
case false:
// Compile time test that foo == bar
break;
}
您可以这样定义自己的 static 断言:
#include <iostream>
template <bool b> class ClassStaticAssert;
template <>
class ClassStaticAssert<true>{static const bool value = true;};
#define STATIC_ASSERT(e) (ClassStaticAssert<e>())
int main()
{
STATIC_ASSERT(0);
return 0;
}
类似于 iammillind 的解决方案,不幸的是,它只在运行时有用:
template <int A, int B>
class VALUES {
};
// specialization to provide safe passage for equal values
template <int X>
class VALUES<X, X> {
public:
static void MY_VALUES_ARE_EQUAL() {}
};
#define ASSERT_EQUALITY(a, b) \
{ \
typedef VALUES<a, b> COMPILE_TIME_ASSERTION; \
COMPILE_TIME_ASSERTION::VALUES_ARE_EQUAL(); \
}
int main() {
ASSERT_EQUALITY(1, 1); // compiles just fine
ASSERT_EQUALITY(1, 2); // ERROR!
// . . .
}
这样做的好处是它提供了一个很好的编译器消息。 我的编译器告诉我以下内容:
“VALUES_ARE_EQUAL”不是“COMPILE_TIME_ASSERTION {aka VALUES<1, 2>}”的成员
您不需要 typedef。 没有:
“VALUES_ARE_EQUAL”不是“VALUES<1, 2>”的成员
当然,还有很多其他方法可以生成有用的消息。 对于咯咯笑:
// these give use some tips in the compiler warnings
class COMPILE_TIME_EQUALITY_ASSERTION {} compiler_message;
class EQUAL_VALUES_ONLY_PLEASE {};
template <int A, int B>
class VALUES {
public:
static void AreEqual(EQUAL_VALUES_ONLY_PLEASE) {}
};
template <int X>
class VALUES<X, X>
{
public:
static void AreEqual(COMPILE_TIME_EQUALITY_ASSERTION) {}
};
#define ASSERT_EQUALITY(a, b) \
{ \
VALUES<a, b>::AreEqual(compiler_message); \
}
int main() {
ASSERT_EQUALITY(1, 1) // a-okay
ASSERT_EQUALITY(1, 2) // ERROR!
}
我收到以下编译器错误:
no matching function for call to:
‘VALUES<1,2>::AreEqual(COMPILE_TIME_EQUALITY_ASSERTION&)'
candidate is:
static void VALUES<\A, B>::AreEqual(EQUAL_VALUES_ONLY_PLEASE) [with int A = 1, int B = 2]
static 成员函数/构造函数/字段分配/隐私和模板规范的组合可以产生不同的结果,可能更适合您的情况。
template <int a, int b>
inline void static_assert_equal()
{
typedef char enum_values_must_be_equal[a == b ? 1 : -1];
(void) sizeof(enum_values_must_be_equal);
}
int main()
{
enum { foo = 1, bar = 2, fum = foo };
static_assert_equal<foo, fum>(); // compiles ok
static_assert_equal<foo, bar>(); // fails at compile time
return 0;
}
这源自checked_delete
习惯用法。
我建议看一下 Eigen 库 static 断言机制:
你可以做一些预处理器魔法,比如
#define FOO_VALUE 263
#define BAR_VALUE 264
enum {foo=FOO_VALUE, bar=BAR_VALUE}
#if !(FOO_VALUE == BAR_VALUE)
#error "Not equal"
#endif
我会 go 获得可用的 static_asserts 之一。
但只是因为我在写这个之前从未尝试过:
enum { foo=263, bar=264 };
template<bool test>
struct CompileAssert
{
bool assert() {}
};
template<>
struct CompileAssert<false> {}; // fail on false.
int main()
{
CompileAssert<foo != bar>().assert(); // Now I have seen Chad above I like his static
CompileAssert<foo == bar>().assert(); // method better than using a normal method.
} // But I tried zero length arrays first did
// not seem to work
template<int X, int Y>
struct Check
{
enum { value = false };
};
template<int X>
struct Check<X,X>
{
enum { value = true };
};
我以int
为例。 您可以根据需要进行更改。 这是演示。 用法:
Check<foo, bar>::value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.