c++ - Uniform initialization with ternary operator return from function -


i don't know if compiler bug (gcc 4.8 on arch linux) or problem standard, code below fails compile. why getfoo1 allowed not getfoo2?

struct foo {     int _i;     foo(int i):_i(i) { } };  foo getfoo1(int i) {     if(i == 3) {         return { + 2 };     } else {         return { };     } }  foo getfoo2(int i) {     return == 3 ? { + 2 } : { }; }  int main() {     auto foo1 = getfoo1(3); //fine     auto foo2 = getfoo2(3); //oops     return 0; } 

braces per se not form expressions (although elements of initializer list expressions). braced-init-list language construct can used initialization in contexts specified § 8.5.4 of c++11 standard (including return statements).

if want return statement compile , still make use of ternary operator and of list initialization, have rewrite way:

return { == 3 ? + 2 : }; 

notice, however, above not necessary. as mentioned david rodriguez - dribeas in comments), give using list initialization. work equally well:

return == 3 ? + 2 : i; 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -