Ternary operator + C++11 constructor from initializer_list
Standard writes in 8.5.4.1: List-initialization
Note: List-initialization can be used
- as the initializer in a variable definition (8.5)
- as the initializer in a new expression (5.3.4)
- in a return statement (6.6.3)
- as a function argument (5.2.2)
- as a subscript (5.2.1)
- as an argument to a constructor invocation (8.5, 5.2.3)
- as an initializer for a non-static data member (9.2)
- in a mem-initializer (12.6.2)
- on the right-hand side of an assignment (5.17)
Nothing of them is a ternary operator. The more minimalistic return 1?{}:{};
is invalid too, what you want is impossible.
Of course you can explicitly call the constructor std::list<std::string>{}
, but I would recommend to write out the if
-else
-block as you already did.
When you do {}
the compiler has no knowledge of the type you are expecting, so it's just a meaningless expression that the compiler doesn't know what to do with. Both sides of the :
are evaluated separately, and only then will the compiler complain if the types don't match. I would just do this:
return generator ? generator() : std::list<std::string>();
If you really like the ternary operator, you can try something like this:
return generator ? generator() : decltype(generator()) { "default value", "generator was empry" };
it will work even if you change the return types later.