【问题标题】:Alias template of a class given as a template parameter c++作为模板参数c ++给出的类的别名模板
【发布时间】:2018-08-24 14:06:32
【问题描述】:

如何将作为模板参数给定的类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&lt;class T&gt; typename _A::Container&lt;T&gt;typename _A::template Container...),但 g++ 给出了 “模板参数 1 无效” “类型/值不匹配”

【问题讨论】:

    标签: c++ templates metaprogramming template-meta-programming


    【解决方案1】:

    正确的语法是:

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

    LIVE

    BTW:不要使用_A 作为模板参数的名称,identifiers beginning with an underscore followed immediately by an uppercase letter 在 C++ 中是保留的。

    【讨论】:

      猜你喜欢
      • 2016-06-01
      • 1970-01-01
      • 2016-11-18
      • 2014-01-29
      • 1970-01-01
      • 2017-09-22
      相关资源
      最近更新 更多