如何将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 无效”或“类型/值不匹配”!
正确的语法是:
template <class A>
struct C : public B< A::template Container >
{
};
顺便说一句:不要_A
用作模板参数的名称,在 C++ 中保留以下划线开头后跟大写字母的标识符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句