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

后端 未结 2 1610
情书的邮戳
情书的邮戳 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条回答
  •  生来不讨喜
    2021-02-12 19:53

    Clang is the only compiler that is correct. Yes, really.

    When the compiler sees a template name without template parameters, it has to look at the deduction guides of the template and apply them to the arguments in the braced-init-list. initializer_list doesn't have any explicit deduction guides, so it uses the available constructors.

    The only publicly accessible constructors that an initializer_list has are its copy/move constructors and its default constructor. Creating a std::initializer_list from a braced-init-list isn't done through publicly accessible constructors. It's done through list-initialization, which is a compiler-only process. Only the compiler can perform the sequence of steps needed to build one.

    Given all of this, it should not be possible to use CTAD on initializer_lists, unless you're copying from an existing list. And that last part is probably how the other compilers make it work in some cases. In terms of deduction, they may deduce the braced-init-list as an initializer_list itself rather than as a sequence of parameters to apply [over.match.list] to, so the deduction guide sees a copy operation.

提交回复
热议问题