Variadic template as template parameter: deduction works with GCC but not with Clang

前端 未结 1 452
余生分开走
余生分开走 2021-02-18 14:25

While compiling some C++11 code with both GCC 4.7.2 and Clang 3.1, I ran into a problem with Clang not managing to deduce a template argument where GCC succeeds. In a more abstr

相关标签:
1条回答
  • 2021-02-18 15:04

    Clang is trying to deduce the arguments for this call:

    processVariadic(SecondContainer<Element>{});
    

    Since SecondContainer has a default template argument, this is equivalent to:

    processVariadic(SecondContainer<Element, Element>{});
    

    Thus, it performs template argument deduction with P = Container<Element> and A = SecondContainer<Element, Element>. It can immediately deduce that the Container template parameter is SecondContainer.

    Next, it considers the template arguments. Since the argument type is fully resolved, Clang believes that the parameter must have as many types, or deduction cannot possibly succeed (it doesn't take default arguments into account). So it flags a deduction failure.


    So, what's supposed to happen? In the words of [temp.deduct.type]p8,

    A template type argument T, a template template argument TT or a template non-type argument i can be deduced if P and A have one of the following forms:
    [...]
    TT<T>
    TT<i>
    TT<>
    where [...] <T> represents template argument lists where at least one argument contains a T, <i> represents template argument lists where at least one argument contains an i and <> represents template argument lists where no argument contains a T or an i.

    In order to match the template arguments, we turn to [temp.deduct.type]p9:

    If P has a form that contains <T> or <i>, then each argument Pi of the respective template argument list P is compared with the corresponding argument Ai of the corresponding template argument list of A.

    There are two things to observe here. One is that this rule does not say what happens if the list Pi and Ai are different lengths (as they are in this case), and the common interpretation seems to be that the mismatched items are not examined. The other is that this rule should not be followed anyway, since the form of P does not contain <T> or <i> (it just contains <>, because there are no template parameters in it).


    So, Clang was wrong to reject this code. I've fixed it in r169475.

    0 讨论(0)
提交回复
热议问题