如何引用作为模板参数的类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;
}
我尝试了几种解决方案,在语句的每个可能位置(例如template
,template<class T> typename _A::Container<T>
...)添加了typename _A::template Container
关键字,但是g++
给出了“模板参数1无效” 或“类型/值不匹配” !
答案 0 :(得分:4)
正确的语法为:
template <class A>
struct C : public B< A::template Container >
{
};
顺便说一句:不要使用_A
作为模板参数的名称,identifiers beginning with an underscore followed immediately by an uppercase letter在C ++中是保留的。