如何使"static_assert"打印失败时获得的值?
How to make the `static_assert` print the value it got on failure?
运行这个:
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
template< int PathLength >
constexpr const int startfindlastslash(const char (&path)[PathLength]) {
return PathLength;
}
int main(int argc, char const *argv[])
{
STATIC_ASSERT( startfindlastslash( "cppdebugger/test_debugger.cpp" ) == 11 );
}
你得到了:
g++ -o main.exe --std=c++14 test_debugger.cpp
test_debugger.cpp: In function ‘int main(int, const char**)’: test_debugger.cpp:1:28: error: static assertion failed: startfindlastslash( "cppdebugger/test_debugger.cpp" ) == 11 #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) ^ test_debugger.cpp:10:5: note: in expansion of macro ‘STATIC_ASSERT’ STATIC_ASSERT( startfindlastslash( "cppdebugger/test_debugger.cpp" ) == 11 ); ^~~~~~~~~~~~~
clang++ -Xclang -ast-print -fsyntax-only --std=c++14 test_debugger.cpp > main.exe
test_debugger.cpp:10:5: error: static_assert failed due to requirement 'startfindlastslash("cppdebugger/test_debugger.cpp") == 11' "startfindlastslash( "cppdebugger/test_debugger.cpp" ) == 11" STATIC_ASSERT( startfindlastslash( "cppdebugger/test_debugger.cpp" ) == 11 ); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test_debugger.cpp:1:28: note: expanded from macro 'STATIC_ASSERT' #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) ^ ~~~~~~~~~~~ 1 error generated.
编译器不会告诉哪个是它应该获得的值,它只说值不相等。
我经常发现将static_asserts
包装在函数模板中很有用。然后编译器将至少在错误消息中打印函数模板参数。如果将此策略与@Phil1970的价值模板方法相结合,您将获得相当不错的结果。
使用 GCC 编译时,以下代码片段会打印包含值的错误消息:In instantiation of 'constexpr void assert_equal(const_val<T, A>, const_val<U, B>) [with T = int; T A = 30; U = int; U B = 11]'
叮当更好:<source>:13:5: error: static_assert failed due to requirement '30 == 11' "Values are not equal!"
.
#define MAKE_CONST(x) const_val<decltype(x), x>{}
#define STATIC_ASSERT_EQUAL(x, y) assert_equal(MAKE_CONST(x), MAKE_CONST(y));
template<typename T, T val>
struct const_val {
constexpr const_val() = default;
constexpr const_val(T v) {}
};
template<typename T, T A, typename U, U B>
constexpr void assert_equal(const_val<T, A>, const_val<U, B>) {
static_assert(A == B, "Values are not equal!");
}
template< int PathLength >
constexpr const int startfindlastslash(const char (&path)[PathLength]) {
return PathLength;
}
int main(int argc, char const *argv[])
{
STATIC_ASSERT_EQUAL(startfindlastslash( "cppdebugger/test_debugger.cpp" ), 11);
}
它不完全是您使用的语法,为了方便起见,它涉及一个额外的宏,但希望它足以满足您的目的......
实时代码在这里。
[ 这不是"如何使static_assert打印值"的真正答案,而是解释为什么 OP 会得到他的行为。
在 c++17 之前,static_assert
采用两个参数。
第一个是要计算的表达式,第二个是在表达式未计算为 true 时要打印的消息。
当您定义STATIC_ASSERT
宏时,您正在static_assert
提供您想要打印的消息 - 它正在为您打印该消息。
- 如何在 assert() 和 static_assert() 之间调度,如果在 constexpr 上下文中依赖?
- 我关于函数"Assert"的C++代码有问题
- assert() 在发布模式下充当标识函数吗?
- 如何编写一个接受如下断言消息的自定义断言函数:assert(false) << "assertio
- 'char (*RtlpNumberOf(T (&)[N]))[N]':无法从"char *"
- Arduino IDE 错误 - 无法找到数字文字运算符"运算符""f900ff"
- "inline"、"constexpr"或"noexcept"
- 我可以禁用 GCC 的 assert() 的 noreturn 属性吗?
- "非静态数据成员之前需要构造函数" - 我是否使用"boost::variant"
- 我必须安装"libbitcoin-server"才能包含"bitcoin.hpp"
- 能够使用debug.assert()函数需要什么
- “并非所有控制路径都返回一个值./&quot“控制可能达到非空隙功能的末端”.验证时循环时
- 如何输出a'&quot在不使用线端的情况下进行文件
- (公开简历)ASSERT: "false" in file qasciikey.cpp
- ASSERT: "allArguments.size() == origArgc"
- 删除“按任何键” ..&quot.从VS C 控制台应用程序
- Qt5 - ASSERT: "bytesTransferred == writeChunkBuffer.size()"
- 如何检测我何时向可变参数函数传递"std::string"而不是"c_str()"
- VS2017 MFC Assert: f:ddvctoolsvc7libsshipatlmfcinclude
- C++ EXPECT_EQ & ASSERT inside for loop