Freedom
Freedom

Reputation: 212

C++ initializer list & copy constructor

I am facing an issue using the {} syntax alongside with the copy constructor. The following code according to Stroustrup should work (I checked his book and he does this very same example multiple times) but when I try to compile it with gcc it fails (it compiles and works with clang though):

#include <iostream>
#include <string>

struct A
{
    int a;
    std::string s;
};

int main()
{
  A a{7, "stefano"};
  A b{a};
  std::cout << "Hello, " << b.s << "!\n";
}

The compilation error is: error: cannot convert 'A' to 'int' in initialization

Clearly it is trying to use the initializer list as list of parameters (therefore trying to assign struct A to int), but according to the language specs it should be possible to call the copy constructor using the {} (and in fact it works in clang).

Is this a peculiarity of gcc? Am I missing something?

Edit: Solution Found: It seems is a bug in the standard that should be fixed in C++14 (check the "Errata" section here: http://stroustrup.com/4th.html )

Upvotes: 0

Views: 100

Answers (0)

Related Questions