提供以下代码,在全局范围内,clang-tidy 不会发出警告:
auto test = []{};
但是,在执行以下操作时,它会:
#include <tuple>
auto test = []{
std::tuple t{1, 2, 3};
};
<source>:3:6: warning: initialization of 'test' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp] auto test = []{ ^ /opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/tuple:646:19: note: possibly throwing constructor declared here constexpr tuple(_UElements&&... __elements) ^
将 lambda 标记为noexcept
无济于事。
但是,我不明白为什么这会是一个问题。理论上,只有在调用lambda 时才会发生异常,不是吗?
以下代码不会导致出现警告:
auto test = [] {
throw 0;
};
clang-tidy 是错的,还是我错过了什么?