使用/ typedef的嵌套模板

说我有这个设置:

template<typename T1>
struct A {
  template<typename T2>
  struct B {
    using type = int;
  };
};

我想能够形成一个typedef /使用:

template<typename T1,typename T2>
using type2 = A<T1>::B<T2>::type;
//... and use like
type2<int,char> foo;

GCC complains that I need typename A<T1>::B<T2>::type instead, and afterwards complains that it expects ";" before "<" after B (i.e. typename A<T1>::B)

没有办法对嵌套模板使用“使用”?

评论
  • 负能量
    负能量 回复

    Note that B is a templated class and type is enclosed in a templated class, hence use the following

    #include <iostream>
    template<typename T1>
    struct A {
        template<typename T2>
        struct B {
            using type = int;
        };
    };
    
    template<typename T1,typename T2>
    using type2 = typename A<T1>::template B<T2>::type;
    
    int main()
    {
        type2<int,char> foo =2;
        std::cout << foo;
    
    }
    
  • Guy
    Guy 回复

    从切换

    using type2 = A<T1>::B<T2>::type;
    

    using type2 = typename A<T1>::template B<T2>::type;