如何引用类的别名模板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
作为模板参数的名称,identifiers beginning with an underscore followed immediately by an uppercase letter在 C++ 中保留。
关于c++ - 作为模板参数 C++ 给出的类的别名模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006225/