T.L*_*T.L 2 c++ templates metaprogramming template-meta-programming
如何引用作为模板参数给出的类的别名模板到从模板基类继承的A类?CB
#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;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几种解决方案,通过template在语句中的每个可能的位置添加关键字(例如template<class T> typename _A::Container<T>,typename _A::template Container...),但g++给出“模板参数 1 无效”或“类型/值不匹配”!
正确的语法是:
template <class A>
struct C : public B< A::template Container >
{
};
Run Code Online (Sandbox Code Playgroud)
BTW:不要用作_A模板参数的名称,以下划线开头紧跟大写字母的标识符在 C++ 中是保留的。