简体   繁体   English

用另一个类的模板参数实例化一个模板类

[英]instantiate a template class with a template parameter of another class

Is there a way to instantiate a template class ("A" in the example) with the template parameter of another class? 有没有一种方法可以使用另一个类的template参数实例化一个模板类(示例中为“ A”)?

example: 例:

class "A": 类别“ A”:

//A.h
template <size_t size> 
class A
{
  void doSmt()
  {
     // do something with size
  }
};

class "B": 类别“ B”:

//B.h
#include "A.h"
template<typename V>
class B
{
  void doSmt2(A<V> a)  //Error Here
  {
    //do something with a
  }
};

Error I got: Error 1 我收到的错误:错误1

error C2993: 'V' : illegal type for non-type template parameter 'size'  

Yes. 是。 Your issue is that V is a type parameter, whereas size is a size_t parameter. 您的问题是V是类型参数,而sizesize_t参数。 Just make them match. 只是使它们匹配。

template <std::size_t V>
class B
{
    void doSmt2(A<V> a)
    {
    }
};

The template type of A is a non-type template parameter. A的模板类型是非类型模板参数。 In B we have a type template parameter. B我们有一个类型模板参数。 These are not compatible. 这些不兼容。 What you need to do is add the non-type template parameter type to B 您需要做的是将非类型模板参数类型添加到B

template<size_t size>
class B
{
  void doSmt2(A<size> a)
  {
    //do something with a
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CRTP 多级 inheritance 用另一个模板参数实例化中间 class - CRTP multiple level inheritance instantiate intermediate class with another template parameter 用另一个类模板实例化一个类模板会导致使用不完整的类型 - Instantiate a class template with another class template leads to using incomplete type 如何实例化模板类的模板 - how to instantiate a template of template class 使用模板类的成员实例化MSVC ++中的模板默认参数 - Using member of template class to instantiate template default parameter in MSVC++ 如何使用 integer 模板参数实例化模板 class 的 static 成员? - How to instantiate a static member of a template class with integer template parameter? 如何使用pack作为模板参数实例化专用模板类? - how to instantiate specialized template class with pack as template parameter? 如何使用另一个模板作为参数初始化模板类? - How to initialize template class with another template as parameter? 具有模板参数的类成员,该成员包含另一个具有参数的模板类 - Class member with template parameter that contains another template class with parameter 模板类作为模板类参数 - Template class as template class parameter 如何使用当前类模板作为另一个模板的模板参数? - How to use the current class template as template parameter for another template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM