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

TL

如何将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 无效”“类型/值不匹配”

songyuanyao

正确的语法是:

template <class A>
struct C : public B<   A::template Container  >
{
};

居住

顺便说一句:不要_A用作模板参数的名称,在 C++ 中保留以下划线开头后跟大写字母的标识符

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章