为什么 g++5 在自动类型推导中推导对象而不是 initializer_list

Why does g++5 deduces object instead of initializer_list in auto type deduction

我最近发现了这段代码:

struct Foo{};

int main() 
{
    Foo a;
    // clang++ deduces std::initializer_list
    // g++5.1 deduces Foo
    auto b{a}; 
    a = b;
}

它在 g++5.1 中编译良好,但在 clang++ 中失败(同时使用 -std=c++11-std=c++14,结果相同)。原因是clang++ deduces the type of b as std::initializer_list<Foo>, whereas g++5.1 deduces as FooAFAIK,类型确实应该是(确实违反直觉)std::initializer_list 这里。为什么 g++5 将类型推断为 Foo

有一个针对 C++1z 的提案实现了大括号初始化的新类型推导规则 (N3922),我猜 gcc 实现了它们:

For direct list-initialization:
1. For a braced-init-list with only a single element, auto deduction will deduce from that entry;
2. For a braced-init-list with more than one element, auto deduction will be ill-formed.

[Example:

auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
auto x2 = { 1, 2.0 }; // error: cannot deduce element type
auto x3{ 1, 2 }; // error: not a single element
auto x4 = { 3 }; // decltype(x4) is std::initializer_list<int>
auto x5{ 3 }; // decltype(x5) is int. 

-- end example]

这是 gcc patch 关于 "Unicorn initialization."

的新变化