Search code examples
c++c++17template-meta-programming

How to use template template as template argument properly?


I have this code with error:

template <typename T1>
struct A {
    template <typename T2>
    struct B {
        using type = char /* actually some class depending on T1 and T2 */;
    };
    template <typename T2>
    using type = B<T2>;
};

template <template <typename> class TT>
struct C {
    template <typename T>
    using type = TT<T> /* actually something more complex */;
};

template <typename T>
struct D {
    using type = typename C<typename A<T>::type>::type;
//                                             ^
// g++: error: type/value mismatch at argument 1 in template parameter list
// for ‘template<template<class> class TT> struct C’
};

int main() {}

If I'm not mistaken, the problem is that A<T>::type is template <class> class, not simple typename as I declared. I tried remove keyword typename after A<T>::type or replace it on template or template <class> class, but it doesn't work.

How can I solve this problem? I don't want significantly edit code of structs and really don't want declare A with 2 template arguments or define B outside A or something else which significantly change A and B due to logic of program but ready consider any helpful idea :)


Solution

  • You need template, but in a different place:

    using type = typename C<A<T>::template type>::type;
    //                            ^~~~~~~~
    

    IIRC, it becomes optional and deprecated in C++23 (that is, template followed by an identifier without <...> after it).