Is std::initializer_list{x, y, z} (CTAD) valid?

后端 未结 2 1645
情书的邮戳
情书的邮戳 2021-02-12 19:32

When constructing an std::initializer_list explicitly, can the template argument (U) be deduced (using class template argument deduction (CTAD

2条回答
  •  萌比男神i
    2021-02-12 19:59

    This is a Clang bug, as concluded in the comment of the never fixed answer of Nicol Bolas. To summarize, as any class type, std::initializer_list has a compiler provided deduction guide [over.match.class.deduct]§1.3

    An additional function template derived as above from a hypothetical constructor C(C), called the copy deduction candidate.

    Which means std::initializer_list has this deduction guide implicitly declared by the compiler:

    template 
    initializer_list (initializer_list ) -> initializer_list ;
    

    When deducing T for this deduction guide with a non empty initalizer list argument, the following rule of the standard is applied [temp.deduct.call]§1:

    If removing references and cv-qualifiers from P gives std::initializer_list or P′[N] for some P′ and N and the argument is a non-empty initializer list ([dcl.init.list]), then deduction is performed instead for each element of the initializer list independently, taking P′ as separate function template parameter types P′i and the ith initializer element as the corresponding argument.

    So T should be deduced to int and so class template argument deduction shall succeed.

    Disclaimer: I am not a Clang advocate...

提交回复
热议问题