声明多个变量,并从默认参数中自动推导模板类型

Fed*_*dor 6 c++ lambda templates language-lawyer template-argument-deduction

在以下程序中,struct B由具有 lambda 对象默认值的非类型模板参数进行参数化:

template <auto = []{}>
struct B {};

// ok everywhere
[[maybe_unused]] B<> b1, b2;
static_assert( std::is_same_v<decltype(b1), decltype(b2)> );

// ok in GCC only
[[maybe_unused]] B b3, b4;
static_assert( !std::is_same_v<decltype(b3), decltype(b4)> );
Run Code Online (Sandbox Code Playgroud)

如果两个变量被声明为B<> b1, b2那么它们都具有相同的类型。但如果声明了 B b3, b4,那么 GCC 就会使它们成为不同的类型(每个类型都有自己的 lambda)。同时MSVC和Clang拒绝接受。MSVC 错误:

<source>(11): error C3538: in a declarator-list 'B' must always deduce to the same type
<source>(11): note: could be 'B<<lambda_2_>{}>'
<source>(11): note: or       'B<<lambda_3_>{}>'
Run Code Online (Sandbox Code Playgroud)

Clang 错误更令人困惑:

error: template arguments deduced as 'B<{}>' in declaration of 'b3' and deduced as 'B<{}>' in declaration of 'b4'
Run Code Online (Sandbox Code Playgroud)

演示: https: //gcc.godbolt.org/z/fcfW6a3es

这里是哪个编译器?