[英]Templated static member functions in C++
I've written a simple test program to try to learn how to use template static member functions in C++. 我编写了一个简单的测试程序,试图学习如何在C ++中使用模板静态成员函数。 The code compiles, but doesn't work right (prints out some garbage).
代码编译,但不能正常工作(打印出一些垃圾)。 I guess I'm using the right syntax.
我想我正在使用正确的语法。 I've read this or this and some other stuff but still don't know what I'm doing wrong.
我已经读过这个或者这个和其他一些东西,但仍然不知道我做错了什么。 The code below:
代码如下:
#include <iostream>
using namespace std;
class Util {
public:
Util();
virtual ~Util();
template <typename T> static void printTab(T tab[]);
};
template <typename T>
void Util::printTab(T tab[]) {
for (unsigned int i=0; i<sizeof(tab)/sizeof(tab[0]); i++) {
cout << tab[0] << " ";
}
cout << endl;
}
int main() {
float tabFloat[5] {1, 2, 3, 4, 5};
unsigned char tabChar[3] {1, 2, 3};
Util::printTab(tabFloat);
Util::printTab(tabChar);
return 0;
}
Any hints appreciated. 任何提示赞赏。
You need to pass the size as another template argument : 您需要将大小作为另一个模板参数传递:
#include <iostream>
using namespace std;
class Util {
public:
Util();
virtual ~Util();
template <typename T,int N> static void printTab(T (&tab)[N])
{
for (int i=0; i<N; i++) {
cout << tab[i] << " ";
}
cout << endl;
}
};
int main() {
float tabFloat[5] {1, 2, 3, 4, 5};
unsigned char tabChar[3] {1, 2, 3};
Util::printTab(tabFloat);
Util::printTab(tabChar);
}
sizeof(tab)
is the size of a T*
, it will not return the size of the whole array. sizeof(tab)
是T*
的大小,它不会返回整个数组的大小。 You need to pass that in yourself as another argument to the function. 你需要将自己作为另一个参数传递给函数。 See here for an explanation and another potential workaround: When a function has a specific-size array parameter, why is it replaced with a pointer?
请参阅此处获取解释和其他可能的解决方法: 当函数具有特定大小的数组参数时,为什么它会被指针替换?
Note that the second printTab
will not output readable characters. 请注意,第二个
printTab
不会输出可读字符。 If you want to see something printed out, try with: 如果您想看到打印出来的东西,请尝试:
unsigned char tabChar[3] {'1', '2', '3'};
How about trying, you need to send the size of the array when calling a function: 怎么样尝试,你需要在调用函数时发送数组的大小:
#include <iostream>
using namespace std;
class Util {
public:
Util();
virtual ~Util();
template <typename T> static void printTab(T tab[], size_t sz);
};
template <typename T>
void Util::printTab(T tab[], size_t sz) {
for (unsigned int i=0; i<sz; i++) {
cout << tab[i] << " ";
}
cout << endl;
}
int main() {
float tabFloat[5] {1, 2, 3, 4, 5};
unsigned char tabChar[3] {1, 2, 3};
Util::printTab(tabFloat, sizeof(tabFloat)/sizeof(float));
Util::printTab(tabChar, sizeof(tabChar)/sizeof(char));
return 0;
}
I'd pass the number of elements of T as a function argument or use a STD container such as a Vector. 我将T的元素数作为函数参数传递或使用STD容器(如Vector)。
Your for
loop is just printing the first element tab[0]
not tab[i]
你的
for
循环只是打印第一个元素tab[0]
而不是tab[i]
Your initialization of tabFloat and tabChar are missing =
您缺少tabFloat和tabChar的初始化
=
float tabFloat[5] {1, 2, 3, 4, 5};
unsigned char tabChar[3] {1, 2, 3};
(also I'd use 65, 66, 67 instead of 1,2,3 for console readability in your testing) (在我的测试中,我还会使用65,66,67而不是1,2,3来控制台的可读性)
float tabFloat[5] = {1, 2, 3, 4, 5};
unsigned char tabChar[3] = { 65, 66, 67};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.