fork download
  1. #include <initializer_list>
  2.  
  3. struct Foo {
  4. Foo(int x_, int y_) : x(x_), y(y_) {}
  5. int x;
  6. int y;
  7. };
  8.  
  9. struct Bar {
  10. Bar(Foo first_, Foo second_) : first(first_), second(second_) {}
  11. Foo first;
  12. Foo second;
  13. };
  14.  
  15. #include <iostream>
  16.  
  17. int main() {
  18. auto list1 = std::initializer_list<int>{ 1, 2 };
  19. Bar b = { list1, list1 };
  20. std::cout << b.first.x << ", " << b.first.y << ", ";
  21. std::cout << b.second.x << ", " << b.second.y << std::endl;
  22. // ^ Output: 1, 2, 3, 4
  23. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:19:28: error: could not convert ‘{list1, list1}’ from ‘<brace-enclosed initializer list>’ to ‘Bar’
     Bar b = { list1, list1 };
                            ^
stdout
Standard output is empty