c++ - C++中的模板模板参数。使用实例

标签 c++ templates parameters instance

我实例化了一个模板并想将该实例用于其他模板

template <typename I> class A
{
  I name;
  public:
  A(I n){
    name = n;
  }

  void show() const
  {
   cout << name << "\t";
  }
};

template <template<typename I> class V> class B {
V origen;
public:
B (V o){
   origen = o;
}   

};

auto main() -> int
{
      cout << "[code.cpp]" << endl;

    A<int> a1(1);
    a1.show();
    B<A> b1(a1);
    return 0;
}

关于这个主题,我看到的是通常不使用,但在使用时,不以这种形式使用,我不明白为什么。 我尝试使用常量。不工作

最佳答案

V origen;void B (V o) 无效,因为 V 不是类型。

也许你想要

template <typename T> class B;

template <template<typename> class V, typename T>
class B<V<T>>
{
    V<T> origen;
public:
    B (V<T> o) : origen(o) {}
};

然后

A<int> a1(1);
B<A<int>> b1(a1);

或者也许

template <template<typename> class V>
class B
{
    V<int> origen;
public:
    B (V<int> o) : origen(o) {}   
};

然后

A<int> a1(1);
B<A> b1(a1);

关于c++ - C++中的模板模板参数。使用实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50457832/

相关文章:

C++模板函数指针

scala - 函数参数: upper bound vs parent class as argument?

php - 将多个参数传递给 PHP 中的方法

c++ - 为什么 getline 的字符串版本是非成员函数?

c++ - 在手写解析器中翻译语法文件

c++ - 如何避免 expr 中的溢出。 A B C D

c++ - 在构造函数中,我可以获取成员的地址并相信它们将保持不变吗?

c++ - 如何为STL容器指定模板函数?

c++ - 如何在 Visual Studio 2017 中支持 Variadic 模板类型

performance - 忽略 T-SQL 中的 NULL 参数