dimanche 1 novembre 2015

why it isn't allowed to use auto & list initialization together before g++ 5.1.0?

Consider following program:

#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;
}

My compiler g++ 4.8.1 gives following compilation errors (I've only included error messages but to see full diagnosis shown by compiler see here):

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>')

I've tried it on g++ 4.8.1, 4.8.2, 4.9.0, 4.9.1 & 4.9.2 & all gives same compilation error as I shown above. But g++ 5.1.0 & later compiles this program successfully & gives desired output. (See live demo here).

So, is this bug in g++ or something else? Why this program was rejected by compilers before g++ 5.1.0?

Aucun commentaire:

Enregistrer un commentaire