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

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

如何引用类的别名模板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 无效”“类型/值不匹配”!

最佳答案

正确的语法是:

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

LIVE

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

关于c++ - 作为模板参数 C++ 给出的类的别名模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006225/

相关文章:

c++ - C++中的自动构造函数生成?

c++ - 找出数组中最小的数及其所在的位置

c++ - 选择模板参数包中的每个偶数(或奇数)参数

ruby - Ruby 中元编程的常见/值得注意的用途是什么?

c++ - 在 wxWidgets listctrl 中禁用编辑

c++ - 可变参数模板和 C 数组

templates - 文本模板中的线程安全映射

css - razor如何用于扩展CSS模板?

c++ - 从内部类引用模板

Java 代码生成(元编程、反射、wtv)