Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor and initializer_list

Could somebody tell me the theory behind this?

Why the last call doesn't compile?

test.cc: In function ‘int main()’: test.cc:15:12: error: too many braces around initializer for ‘int’ [-fpermissive] test.cc:15:12:

error: invalid conversion from ‘’ to ‘int’ [-fpermissive] test.cc:9:6: error: initializing argument 1 of ‘void f(std::initializer_list)’ [-fpermissive] test.cc:15:12:

error: aggregate value used where an integer was expected

I think either c++11 or g++ 4.7 is broken on this. Thank you!

#include <initializer_list>

class A { 
  public:
  A(const std::initializer_list<int>) {}
};

void f(const std::initializer_list<int>) {}

int main() {
  A({1});  // Compile OK
  f({1});  // Compile OK
  A({{{1}}});   // Compile OK
  //f({{{1}}}); // Compile Error.
}
like image 525
yufanyufan Avatar asked Apr 13 '26 22:04

yufanyufan


1 Answers

Here is what I believe GCC is thinking.

This is your program with 1 extra line, and the interesting lines numbered.

int main() {
  A({1});  // 1. Compile OK
  f({1});  // 2. Compile OK
  A{{1}};  // 3. Compile OK, equivalent to 1. 
  A({{{1}}});   // 4. Compile OK
  //f({{{1}}}); // 5. Compile Error.
}

Why does GCC compile 4 but not 5?

For clarity, suppose the construction at #4 actually declared something:

A a{{{1}}};   // 4a. Compile OK

GCC asks if the argument of the constructor, which is {{1}} is implicitly convertible to A. So is:

A{{1}}

a valid conversion from {{1}} to A? Yes it is - as per 3.

This reasoning, of course, is not applicable to #5; hence error.

If you want to stop GCC from accepting #4, then block the enabling conversion by making the enabling constructor explicit:

class A { 
    public:
    explicit A(const std::initializer_list<int> il) {}
};

Then #4 will give the error:

error: converting to ‘A’ from initializer list would use explicit constructor ‘A::A(std::initializer_list<int>)’
like image 145
Mike Kinghan Avatar answered Apr 17 '26 00:04

Mike Kinghan