为什么这个变量在 C++14 中的 g++ 中没有推导为 initializer_list?

why this variable isn't deduced as initializer_list in g++ in C++14?

考虑以下程序:

#include <iostream>
int main()
{
    int n = 3;
    int fact = 1;
    for(auto i{1};i<=n;i++)
        fact*=i;
    std::cout<<"fact of "<<n<<" is "<<fact;
}

即使我使用 -std=c++14 选项,它也能在 ideone 上正常编译。查看现场演示 here. But in C++14 the variable i should be deduced as initializer_list according to

有一个 C++1z 提案实现了大括号初始化的新类型推导规则:

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

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

auto x5{ 3 }; // decltype(x5) is int.

-- end example]

因此,规则在 C++17 中发生了变化。因此,当我使用 -std=c++14 时,程序不应编译。这是g ++中的错误吗?在 C++14 中,变量 i 不应该推导为 initializer_list 吗?

There is a proposal for C++1z that implements new type deduction rules for brace initialization

不完全是。如果您按照 link 阅读实际论文,它会显示:

Direction from EWG is that we consider this a defect in C++14.

这足以让实现者也将其视为缺陷,因此即使在 C++14 模式下也能改变编译器行为。