[英]Passing an instance of a user defined template class to a non templated function
我有以下实现:
#include <cstddef>
template<typename Data, size_t Size>
class Demo
{
public:
Demo();
private:
Data data[Size];
};
void f(Demo<int, size_t>& demoObj)
{
}
int main()
{
Demo<int, 100> demoObj;
}
编译时出现以下错误:
g++ -std=c++11 temp.cpp
temp.cpp:13:24: error: type/value mismatch at argument 2 in template parameter list for ‘template<class Data, long unsigned int Size> class Demo’
void f(Demo<int, size_t>& demoObj)
^
temp.cpp:13:24: note: expected a constant of type ‘long unsigned int’, got ‘size_t {aka long unsigned int}’
这个错误对我来说没有意义。 请帮助我理解它。 另外,如何将demoObj传递给函数f? 我的意思是o如何写f的定义。
Size
是一个非类型参数,因此它需要一个非类型参数:
void f(Demo<int, 100>& demoObj);
// ^^^
如果您希望能够传递任何种类的演示,则可以将f
定义为模板函数。
template<typename Data, size_t Size>
void f(Demo<Data, Size>& demoObj)
{
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.