Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading