关于静态全局lambda变量的错误铿锵声警告?

Asu*_*Asu 5 c++ lambda clang-tidy

提供以下代码,在全局范围内,clang-tidy不提供警告:

auto test = []{};
Run Code Online (Sandbox Code Playgroud)

但是,在执行以下操作时,它会:

#include <tuple>

auto test = []{
    std::tuple t{1, 2, 3};
};
Run Code Online (Sandbox Code Playgroud)
<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)
                  ^
Run Code Online (Sandbox Code Playgroud)

将lambda标记为noexcept没有用.

但是,我不明白为什么会出现这个问题.异常只能在调用 lambda 时理论上发生,不是吗?

以下代码不会导致出现警告:

auto test = [] {
    throw 0;
};
Run Code Online (Sandbox Code Playgroud)

铿锵有误,还是我错过了什么?

JVA*_*pen 1

Clang-Tidy 警告是关于全局变量的构造,而不是关于operator()此类的构造。因此,这看起来像是误报。

我会让变量constexpr因为它在程序的生命周期内无法更改。这也应该抑制警告以及 constexpr 无法完成这样的异常抛出。

PS:您可以在 bugs.llvm.org 记录 Clang-Tidy 的错误