Search code examples
c++visual-c++visual-studio-2022

Template subclass of template class instantiated with value (not a type)


Cannot compile it in Visual Studio 2022 (different subversions).

template <int N>
struct A {
};

template <typename T0>
struct B {
    template <typename T1>
    using C = T1;
};

template <int N>
struct D {
    using E = A<N>;

    template <typename T2>
    using F = typename B<E>::C<T2>; // Error line
};

Generally, all compilers response with:

Error C2988 unrecognizable template declaration/definition
Error C2059 syntax error: '<'
Error C2238 unexpected token(s) preceding ';'

Originally the problem occurred in a bit more complex structure, but I tried to extract it to that simpler snippet.

Putting the word typename does not help!

UPD. Added typename


Solution

  • In the line using F = typename B<E>::C<T2>;, C is a dependent template name.

    To fix this problem, change the line to using F = typename B<E>::template C<T2>;. Compiler explorer

    Clang diagnoses this for you:

    <source>:16:30: error: use 'template' keyword to treat 'C' as a dependent template name
       16 |     using F = typename B<E>::C<T2>; // Error line
          |                              ^
          |                              template 
    

    Compiler explorer