如何使我的C 编译器发出constexpr值(例如警告?)
How to make my C++ compiler emit a constexpr value (e.g. in warning?)
(这是一个XY问题,但请携带我。)
我会收到有关轮班数量太大的汇编警告。现在,为了诊断这一点,我希望我的编译器以某种方式发出用作换档数量的constexpr
值。
到目前为止,我这样做的方式是尝试实例化具有数字参数的类型,我知道我可以将其放置在范围之外,然后添加我想要的constexpr值并获得一个错误,向我显示了总和。但这是一个丑陋的黑客。是否有一种方法可以使ConstexPR值(希望不仅是整数)发出到标准错误流?例如以及一些解释性文字或警告消息?
我询问有关GCC 6.x的及以后的问题,然后再叮当4.x,然后再询问。
好吧,明显的方法与您所说的相似 - 使编译器在发出诊断时提及该值。
constexpr int I = 8 % 3;
template<int i>
class TheValueYouWantIs { static_assert(i != i); };
int main() {
TheValueYouWantIs<I>();
}
因此:
prog.cpp: In instantiation of ‘class TheValueYouWantIs<2>’:
prog.cpp:8:27: required from here
[...less informative stuff...]
警告显然更依赖编译器,但应该很容易。不过,这种事情对您的char阵列无济于事。不是一个完整的解决方案。
gcc显示 <<
的操作数时,它在恒定表达式中发出错误消息时会显示出错误消息。当结果不用用作恒定表达式时,它不会显示<<
的操作数。您可以通过添加一个原本毫无意义的常数来利用它。
template <int> constexpr int f() { return 1; }
template <int> constexpr int g() { return 40; }
template <int I> constexpr int h() { return f<I>() << g<I>(); }
int main() { h<1234>(); }
这会引起警告,而没有有关问题值的信息: warning: left shift count >= width of type [-Wshift-count-overflow]
。
template <int> constexpr int f() { return 1; }
template <int> constexpr int g() { return 40; }
template <int I> constexpr int h() { constexpr int i = f<I>() << g<I>(); return f<I>() << g<I>(); }
int main() { h<1234>(); }
这会导致有关问题值的信息(以及更多警告)的错误: error: right operand of shift expression ‘(1 << 40)’ is >= than the precision of the left operand
。
如果仅第二操作数是一个恒定的表达式,那么它仍然可以,因为此特定警告足以将左操作数变成常数1
。
这是超级态度的,但在可辨别的格式(
)中产生了表达式的名称及其值constexpr int I = 8 % 3;
#define CONCATENATE( s1, s2 ) s1 ## s2
#define EXPAND_THEN_CONCATENATE( s1, s2 ) CONCATENATE( s1, s2 )
template<int i>
class The_expression_named_in_the_previous_error_has_value{ static_assert(i != i, ""); };
#define ERROR_PRINT(_expr)
EXPAND_THEN_CONCATENATE(In_the_next_error_you_will_find_the_value_of_the_expression__, _expr);
The_expression_named_in_the_previous_error_has_value<I>();
int main() {
ERROR_PRINT(I);
}
这会产生(使用GCC 6):
main.cpp: In function ‘int main()’:
main.cpp:11:25: error: ‘In_the_next_error_you_will_find_the_value_of_the_expression__I’ was not declared in this scope
EXPAND_THEN_CONCATENATE(In_the_next_error_you_will_find_the_value_of_the_expression__, _expr);
^
main.cpp:3:45: note: in definition of macro ‘CONCATENATE’
#define CONCATENATE( s1, s2 ) s1 ## s2
^
main.cpp:11:1: note: in expansion of macro ‘EXPAND_THEN_CONCATENATE’
EXPAND_THEN_CONCATENATE(In_the_next_error_you_will_find_the_value_of_the_expression__, _expr);
^
main.cpp:15:5: note: in expansion of macro ‘ERROR_PRINT’
ERROR_PRINT(I);
^
main.cpp: In instantiation of ‘class The_expression_named_in_the_previous_error_has_value<2>’:
main.cpp:15:5: required from here
main.cpp:7:61: error: static assertion failed:
class The_expression_named_in_the_previous_error_has_value{ static_assert(i != i, ""); };
,但我敢肯定,这可以通过一些constexpr-string-trickery进行大规模改进。
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我正在尝试在我的类中创建一个静态成员,但编译器警告我它是未定义的
- 我的代码错误类型警告 1 警告 C4018 和更多错误
- 为什么尽管使用了 -isystem,但 clang 在我的标头上报告警告,而 gcc 没有报告警告?
- 如何使我的C 编译器发出constexpr值(例如警告?)
- 为什么 GCC 在使用继承的构造函数时警告我无用的强制转换
- 在Visual Studio 2012中运行我的OpenCV代码时无法找出'u202A'警告
- 为什么编译器在我定义_CRT_SECURE_NO_WARNINGS之后仍然警告我不安全的strtok
- 我可以使用 CMake 编译具有不同警告级别的项目的不同部分吗?
- 为什么MSVC10忽略我的#pragma警告(禁用:4251)
- 我的For循环出了什么问题?我得到警告:有符号和无符号整数表达式之间的比较[-Wsigncompare]
- 当我向只接受无符号整数的函数提供有符号整数时,是否有一种方法可以使GCC警告我?
- 编译器警告我不明白 - 可能未初始化的 QStringList
- 使用我的数组类时的奇怪警告
- 尝试着创造一款原始的主机蛇游戏.0警告/论述.为什么我的程序崩溃了?(c++)