[英]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
是类型参数,而size
是size_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.