在static_assert()中编译时显示整数 [英] Display integer at compile time in static_assert()
问题描述
以下是我正在尝试做的简化版
Here is a simplified version of what I'm trying to do
enum First
{
a,
b,
c,
nbElementFirstEnum,
};
enum Second
{
a,
b,
c,
nbElementSecondEnum,
};
static_assert(
First::nbElementFirstEnum == Second::nbElementSecondEnum,
"Not the same number of element in the enums.");
/*static_assert(
First::nbElementFirstEnum == Second::nbElementSecondEnum,
"Not the same number of element in the enums." + First::nbElementFirstEnum + " " + Second::nbElementSecondEnum);*/
但我希望能够打印First ::的值断言消息中的nbElementFirstEnum和Second :: nbElementSecondEnum(就像在注释版本中显然不起作用)。
我尝试使用#进行宏连接。
我还尝试使用可变参数模板,使用%10检索每个数字并将0字符添加到检索到的值,但我得到的只是constexpr char []。
But I would like to be able to print the value of First::nbElementFirstEnum and Second::nbElementSecondEnum in the assert message (like in the commented version which obviously doesn't work). I have tryed using macro concatenation with "#". I also tryed using variadic templates, retrieveing with %10 each number and adding the '0' character to the value retrieved, but all I get is a constexpr char[].
所以我的问题是如何让我的枚举值以字符串文字打印。
So my question is how can I get my enums values to be printed in a string literal.
可能重复:
最有趣的话题是这一个:
在编译时打印sizeof(T)
但我不想发出警告或退出代码来了解这些值。
The most interesting topic was this one: Printing sizeof(T) at compile time But I don't want to have a warning, or decomment code to know the values.
推荐答案
这基本上有效,但是只要稍微努力就可以打破(通过使V1和V2总和达到256的倍数)。所以,我认为你的解决方案更加丑陋但更强大。
This basically works, although it's possible to break with a little effort (by making V1 and V2 sum to a multiple of 256). So, I think your solution is uglier but still more robust.
template <int V1, int V2> struct AssertEquality
{
static const char not_equal_warning = V1 + V2 + 256;
};
template <int V> struct AssertEquality<V, V>
{
static const bool not_equal_warning = 0;
};
#define ASSERT_EQUALITY(V1, V2) static_assert( \
AssertEquality<static_cast<int>(V1), \
static_cast<int>(V2)>::not_equal_warning == 0, \
#V1 " != " #V2 );
// ...
ASSERT_EQUALITY(First::nbElementFirstEnum, Second::nbElementSecondEnum);
输出如下:
g++ -std=c++0x -c chksz.cpp
chksz.cpp: In instantiation of ‘const char AssertEquality<3, 2>::not_equal_warning’:
chksz.cpp:40:124: instantiated from here
chksz.cpp:5:53: warning: overflow in implicit constant conversion
chksz.cpp:40:1: error: static assertion failed: "First::nbElementFirstEnum != Second::nbElementSecondEnum"
作为参考,这个原始版本依赖于gcc打印 static_assert
消息,即使布尔条件根本不编译也是如此。
For reference, this original version depended on gcc printing the static_assert
message even when the boolean condition doesn't compile at all.
template <typename Enum1, int Max1, typename Enum2, int Max2>
struct AssertSameSizeEnums;
template <typename Enum1, int EnumMax, typename Enum2>
struct AssertSameSizeEnums<Enum1, EnumMax, Enum2, EnumMax> {};
// only define the special case where Max1 and Max2 have the same integer value
#define ASSERT_SAME_SIZE_ENUMS(E1, M1, E2, M2) static_assert( \
sizeof(AssertSameSizeEnums<E1, E1::M1, E2, E2::M2>), \
#E1 "::" #M1 " != " #E2 "::" #M2 );
enum class First {
a, b, c, nbElementFirstEnum,
};
enum class Second {
a, b, c, nbElementSecondEnum,
};
ASSERT_SAME_SIZE_ENUMS(First, nbElementFirstEnum, Second, nbElementSecondEnum);
注意我将您的枚举更改为强类型,因为否则枚举的常量名称会发生冲突。如果你有弱类型的枚举,传递给宏的第一
和第二
应该命名封闭的范围。
Note I changed your enums to be strongly-typed, because otherwise the enumerated constant names clashed. If you have weakly-typed enums, the First
and Second
passed to the macro should name the enclosing scope.
现在,如果我注释掉其中一个值(因此枚举的大小不同),我得到:
Now, if I comment out one of the values (so the enums are different sizes), I get:
g++ -std=c++0x -c chksz.cpp
chksz.cpp:25:113: error: invalid application of ‘sizeof’ to incomplete type ‘AssertSameSizeEnums<First, 3, Second, 2>’
chksz.cpp:25:1: error: static assertion failed: "First::nbElementFirstEnum != Second::nbElementSecondEnum"
查看完整值在不完整类型错误中的显示方式,以及静态断言中的符号名称?
See how the integer values are displayed in the incomplete type error, and the symbolic names in the static assertion?
这篇关于在static_assert()中编译时显示整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!