Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is initializer_list allowed to be deduced?

I know there are special rules for when initializer_list can be deduced but until discovering the following I somehow thought that was never. What are the rules for when it is OK to deduce or omit initializer_list ?

The following example seems ilogical and feels almost like a language deficit ?

#include <initializer_list>

void test() {
    bool reverse = true;
    const auto ascend = {1,2,3};//OK : seems to deduce to std::initializer_list<const int>

    //const auto a_or_d_AUTO = reverse ? {3,2,1} : {1,2,3};//not ok, why ?

    const auto i = reverse ? 3 : 1;// also fine

    const auto a_or_d = reverse ? std::initializer_list<const int>({3,2,1}) : std::initializer_list<const int>({1,2,3});//also OK
}

https://godbolt.org/z/1sNcu4

like image 776
darune Avatar asked Dec 14 '22 13:12

darune


1 Answers

This has nothing to do with deduction. The grammar for ?: requires actual expressions for all three operands:

[expr.cond]:

conditional-expression:
    logical-or-expression
    logical-or-expression ? expression : assignment-expression

A braced-init-list is not an expression and simply can't be used with ?:.

like image 160
T.C. Avatar answered Dec 26 '22 00:12

T.C.