作为模板参数 c++ 给出的 class 的别名模板
Alias template of a class given as a template parameter c++
如何将作为模板参数给定的 class A
的别名模板引用到继承自模板基 [=29] 的 class 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++
给出了"template argument 1 is invalid" 或 "type/value mismatch"!
正确的语法是:
template <class A>
struct C : public B< A::template Container >
{
};
顺便说一句:不要使用 _A
作为模板参数的名称,identifiers beginning with an underscore followed immediately by an uppercase letter 在 C++ 中保留。
如何将作为模板参数给定的 class A
的别名模板引用到继承自模板基 [=29] 的 class 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++
给出了"template argument 1 is invalid" 或 "type/value mismatch"!
正确的语法是:
template <class A>
struct C : public B< A::template Container >
{
};
顺便说一句:不要使用 _A
作为模板参数的名称,identifiers beginning with an underscore followed immediately by an uppercase letter 在 C++ 中保留。