Search code examples
c++templatesc++17template-argument-deduction

Deduction guide with type alias after ->


Take a look at this code:

template<class T>
struct S {
    template<class U>
    S(U) {}
};

template<typename T>
using SP = S<T*>;

// template<class U>
// S(U) -> S<U*>;

template<class U>
S(U) -> SP<U>;

S s(0);

Unfortunately, it is rejected by Clang:

error: deduced type 'SP<U>' (aka 'S<type-parameter-0-0 *>') of deduction 
       guide is not written as a specialization of template 'S'
S(U) -> SP<U>;
        ^~~~~~~~~~

ICC rejects it either with a similar error:

error: the return type must directly designate a specialization 
       of the associated class template

However, GCC and MSVC both compile this code.

Which compiler is right? After all, a type alias is just an alias.


Solution

  • Clang and ICC are correct here: the restriction on how to name the template and type is phrased syntactically. C++20 allows “class template” argument deduction for alias templates, but does not support (separate) deduction guides for them.