作为模板参数 c++ 给出的类的别名模板

Alias template of a class given as a template parameter c++

本文关键字:别名 c++ 参数      更新时间:2023-10-16

如何将类的别名模板A作为模板参数提供给从模板基类B继承的类C

#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++保留以下划线开头后跟大写字母的标识符。