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

rdi*_*503 0 c++ templates

有没有办法用另一个类的模板参数实例化模板类(示例中的"A")?

例:

"A"级:

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

"B"级:

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

我得到的错误:错误1

error C2993: 'V' : illegal type for non-type template parameter 'size'  
Run Code Online (Sandbox Code Playgroud)

Tar*_*ama 6

是.你的问题是,V是一种类型的参数,而size是一个size_t参数.只是让它们匹配.

template <std::size_t V>
class B
{
    void doSmt2(A<V> a)
    {
    }
};
Run Code Online (Sandbox Code Playgroud)