Display integer at compile time in static_assert()(在 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.
可能的重复:
C++11 static_assert:参数化错误消息
在 static_assert 输出中集成类型名称?
最有趣的话题是这个:在编译时打印 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);
请注意,我将您的枚举更改为强类型,否则枚举的常量名称会发生冲突.如果你有弱类型枚举,传递给宏的 First
和 Second
应该命名封闭范围.
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() 编译时显示整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 static_assert() 编译时显示整数
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01