Template specialization and alias template deduction difference

前端 未结 1 469
遇见更好的自我
遇见更好的自我 2021-02-14 10:28

I\'m struggling to understand how deduction works in the following case:

template
struct AImpl
{ };

template

        
1条回答
  •  花落未央
    2021-02-14 10:46

    Why a1 is not deduced in this context?

    Because the template argument of doSomething appears in a non-deduced context. An alias template stands almost exactly for what it aliases. And yours is defined as follows:

    template
    using A = typename AHelper::type;
    

    To deduce code, the compiler will need to deduce something on the left of :: and that is a non-deduced context. Template argument deduction won't even attempt to deduce something if it appears as an argument to the left of the scope resolution operator.

    It's not without cause that this is an undeduced context. Remember that templates may be specialized. I could add this:

    template
    struct AHelper
    {
        using type = BImpl; // My own class!
    };
    

    A compiler will need to look at all the code in the entire program and try all the types to be sure nothing nefarious is happening for it to be certain that a1 really matches as typename AHelper::type. That's intractable. So a mapping by meta-functions is a one way street only. You can't ask the compiler to deduce the source type (or non-type argument) from the target type.

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