考虑以下程序:
#include <iostream>
int main()
{
int n;
int fact{1};
std::cout<<"Enter a number: ";
std::cin>>n;
for(auto i{1};i<=n;i++)
fact*=i;
std::cout<<"fact of "<<n<<" is "<<fact;
}
我的编译器 g++ 4.8.1 给出了以下编译错误(我只包含错误消息,但要查看编译器显示的完整诊断,请参见此处):
8 17 [Error] no match for 'operator<=' (operand types are 'std::initializer_list<int>' and 'int')
8 22 [Error] no 'operator++(int)' declared for postfix '++' [-fpermissive]
9 7 [Error] no match for 'operator*=' (operand types are 'int' and 'std::initializer_list<int>')
我已经在 g++ 4.8.1、4.8.2、4.9.0、4.9.1 和 4.9.2 上尝试过,并且都给出了与上面所示相同的编译错误。但是 g++ 5.1.0 及更高版本成功编译了该程序并提供了所需的输出。(在此处查看现场演示)。
那么,这个错误是 g++ 还是其他什么?为什么这个程序在 g++ 5.1.0 之前被编译器拒绝?