Search code examples
c++c++11templatestemplate-templates

Unzip template-template parameter from type bundle


How to fix the using line?

test_temp.cpp:

#include <memory>
template <typename A, typename B, template<typename> class C>
class X {};

template <typename A1, typename B1, template<typename> class C1>
class Type_bundle {
public:
    typedef A1 A;
    typedef B1 B;
    template<typename T> using C = C1<T>;
};

template <typename T_b>
using X_b = X<typename T_b::A, typename T_b::B, typename T_b::template C>;


int main() {
    typedef Type_bundle<int,float,std::allocator> Ttb;
    X_b<Ttb> x;
}

Error from clang++

test_temp.cpp:14:63: error: expected an identifier or template-id after '::'
using X_b = X<typename T_b::A, typename T_b::B, typename T_b::template C>;
                                                         ~~~~~^

Why doesn't this work?


Solution

  • When you write:

    template <typename T_b>
    using X_b = X<typename T_b::A, typename T_b::B, typename T_b::template C>;
    //                                              ~~~~~~~~~
    

    That suggests that what follows will be the name of a type. But T_b::template C isn't a type, it's a template, so this construct isn't valid. You'd use that construct if you went on to pass parameters into C, like typename T_b::template C<D>. There, both typename and template are necessary.

    What you want is just:

    template <typename T_b>
    using X_b = X<typename T_b::A, typename T_b::B, T_b::template C>;
    

    No typename.