gpt4 book ai didi

c++ - 在 C++ 中,static 断言当前位置的声明不会包含在任何 namespace 中?

转载 作者:太空狗 更新时间:2023-10-29 20:37:13 25 4
gpt4 key购买 nike

在 C++ 中,有时会出现恼人的错误,即有人忘记关闭在头文件中打开的 namespace ,有时很难准确找到是哪一个。

有点不那么平庸,有时有些宏由于技术原因不能出现在任何命名空间中,或者可能会出现神秘的错误消息。例如,在 boost 融合库中,BOOST_FUSION_DEFINE_STRUCT 需要这个。相反,您应该在文件范围内使用宏,并将它传递给您希望声明所在的 namespace 。

就此而言,如果您在命名空间中包含任何标准库 header ,则会出现未定义的行为。

是否可以做一些事情,比如写一个宏,例如ASSERT_FILESCOPE 这样我就可以在 C++ 头文件的末尾或宏(如 BOOST_FUSION_DEFINE_STRUCT)的开头放置一行 ASSERT_FILESCOPE;,如果该表达式在文件范围内,这将导致 static_assert 失败并显示一条很好的错误消息?

最佳答案

一种可能是声明一个名称然后测试::name。为避免在稍后在同一翻译单元中使用宏之前在全局范围内声明相同的名称,您可以使用 __COUNTER____LINE__ 如果它更适合你。无论如何它都不完美,但至少错误的第一行包含消息。 <知识库> live example

#include <type_traits>

#define CONCAT(x, y) CONCAT_I(x, y)
#define CONCAT_I(x, y) x##y

#define ASSERT_FILESCOPE \
ASSERT_FILESCOPE_I(__COUNTER__)

#define ASSERT_FILESCOPE_I(counter) \
static constexpr bool CONCAT(ASSERTION_FAILED_NOT_AT_FILE_SCOPE, counter)() { return true; } \
static_assert(::CONCAT(ASSERTION_FAILED_NOT_AT_FILE_SCOPE, counter)(), "Detected a location other than file scope.")

我从 Clang 得到这个错误:

main.cpp:17:5: error: no member named 'ASSERTION_FAILED_NOT_AT_FILE_SCOPE0' in the global namespace; did you mean simply 'ASSERTION_FAILED_NOT_AT_FILE_SCOPE0'?
ASSERT_FILESCOPE;
^~~~~~~~~~~~~~~~
main.cpp:7:5: note: expanded from macro 'ASSERT_FILESCOPE'
ASSERT_FILESCOPE_I(__COUNTER__)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:11:19: note: expanded from macro 'ASSERT_FILESCOPE_I'
static_assert(::CONCAT(ASSERTION_FAILED_NOT_AT_FILE_SCOPE, counter)(), "Detected a location other than file scope.")
^~
main.cpp:17:5: note: 'ASSERTION_FAILED_NOT_AT_FILE_SCOPE0' declared here
main.cpp:7:5: note: expanded from macro 'ASSERT_FILESCOPE'
ASSERT_FILESCOPE_I(__COUNTER__)
^
main.cpp:10:20: note: expanded from macro 'ASSERT_FILESCOPE_I'
static constexpr bool CONCAT(ASSERTION_FAILED_NOT_AT_FILE_SCOPE, counter)() { return true; } \
^
main.cpp:3:22: note: expanded from macro 'CONCAT'
#define CONCAT(x, y) CONCAT_I(x, y)
^
main.cpp:4:24: note: expanded from macro 'CONCAT_I'
#define CONCAT_I(x, y) x##y
^
<scratch space>:101:1: note: expanded from here
ASSERTION_FAILED_NOT_AT_FILE_SCOPE0
^

还有这个来自 GCC 的:

main.cpp:11:19: error: '::ASSERTION_FAILED_NOT_AT_FILE_SCOPE0' has not been declared
static_assert(::CONCAT(ASSERTION_FAILED_NOT_AT_FILE_SCOPE, counter)(), "Detected a location other than file scope.")
^
main.cpp:7:5: note: in expansion of macro 'ASSERT_FILESCOPE_I'
ASSERT_FILESCOPE_I(__COUNTER__)
^
main.cpp:17:5: note: in expansion of macro 'ASSERT_FILESCOPE'
ASSERT_FILESCOPE;
^
main.cpp:11:19: note: suggested alternative:
static_assert(::CONCAT(ASSERTION_FAILED_NOT_AT_FILE_SCOPE, counter)(), "Detected a location other than file scope.")
^
main.cpp:7:5: note: in expansion of macro 'ASSERT_FILESCOPE_I'
ASSERT_FILESCOPE_I(__COUNTER__)
^
main.cpp:17:5: note: in expansion of macro 'ASSERT_FILESCOPE'
ASSERT_FILESCOPE;
^
main.cpp:10:34: note: 'foo::ASSERTION_FAILED_NOT_AT_FILE_SCOPE0'
static constexpr bool CONCAT(ASSERTION_FAILED_NOT_AT_FILE_SCOPE, counter)() { return true; } \
^
main.cpp:4:24: note: in definition of macro 'CONCAT_I'
#define CONCAT_I(x, y) x##y
^
main.cpp:10:27: note: in expansion of macro 'CONCAT'
static constexpr bool CONCAT(ASSERTION_FAILED_NOT_AT_FILE_SCOPE, counter)() { return true; } \
^
main.cpp:7:5: note: in expansion of macro 'ASSERT_FILESCOPE_I'
ASSERT_FILESCOPE_I(__COUNTER__)
^
main.cpp:17:5: note: in expansion of macro 'ASSERT_FILESCOPE'
ASSERT_FILESCOPE;
^

关于c++ - 在 C++ 中,static 断言当前位置的声明不会包含在任何 namespace 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35531702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com