When is initializer_list allowed to be deduced?

  • A+
Category:Languages

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

 


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 ?:.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: