作为模板参数给定的类的别名模板C ++

时间:2018-08-24 14:06:33

标签: c++ templates metaprogramming template-meta-programming

如何引用作为模板参数的类A的别名模板,该类C从模板基类B继承?

#include <vector>

struct A
{
    // the alias template I want to refer to:
    template<class T>
    using Container = std::vector<T>;
};

// the base class
template<template<class> class _Container>
struct B 
{
    _Container<int> m_container;
};

template<class _A>
struct C : public B<   typename _A::Container  >
{//                    ^^^^^^^^^^^^^^^^^^^^^^ 

};

int main()
{
    C<A> foo;
}

我尝试了几种解决方案,在语句的每个可能位置(例如templatetemplate<class T> typename _A::Container<T> ...)添加了typename _A::template Container关键字,但是g++给出了“模板参数1无效” “类型/值不匹配”

1 个答案:

答案 0 :(得分:4)

正确的语法为:

template <class A>
struct C : public B<   A::template Container  >
{
};

LIVE

顺便说一句:不要使用_A作为模板参数的名称,identifiers beginning with an underscore followed immediately by an uppercase letter在C ++中是保留的。