fork(1) download
  1. #include <iostream>
  2. #include <initializer_list>
  3. using namespace std;
  4.  
  5. struct InitListConstructible {
  6. InitListConstructible() = default;
  7.  
  8. InitListConstructible(std::initializer_list<int> elems) {
  9. // do something
  10. }
  11. };
  12.  
  13. InitListConstructible operator+ (InitListConstructible lhs,
  14. InitListConstructible rhs) {
  15. return {};
  16. }
  17.  
  18. void freeFunction(InitListConstructible lhs, InitListConstructible rhs) {
  19. // Do nothing
  20. }
  21.  
  22. int main() {
  23. /* Totally fine - use initializer list constructor. */
  24. InitListConstructible lhs = { 1, 2, 3 };
  25.  
  26. /* Totally fine - use overloaded + operator. */
  27. auto legit = lhs + lhs;
  28.  
  29. /* Totally fine - second argument constructed from initializer list. */
  30. freeFunction(lhs, { 1, 2, 3 });
  31.  
  32. /* Totally fine - explicit call to operator+. */
  33. auto alsoLegit = operator+ (lhs, { 1, 2, 3 });
  34.  
  35. /* Not okay - reports error about expected primary-expression. */
  36. auto error = lhs + { 1, 2, 3 };
  37.  
  38. return 0;
  39. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:36:21: error: expected primary-expression before ‘{’ token
  auto error = lhs + { 1, 2, 3 };
                     ^
stdout
Standard output is empty