[英]Working function pointer template gives Netbeans error
I have a set of functions that take no parameters and return an integer, and I want to cache the result of them in C++03. 我有一组不带参数且返回整数的函数,我想将它们的结果缓存在C ++ 03中。
After refreshing my function pointer syntax, I write: 刷新函数指针语法后,我写:
#include <iostream>
int return_one() {
std::cout << "Calling one\n";
return 1;
}
int return_two() {
std::cout << "Calling two\n";
return 2;
}
template <int(*f)()> // line 11
int cached() {
static int x = f(); // line 13
return x;
}
int main() {
std::cout << cached<return_one>() << '\n';
std::cout << cached<return_two>() << '\n';
std::cout << cached<return_one>() << '\n';
std::cout << cached<return_two>() << '\n';
std::cout << cached<return_one>() << '\n';
std::cout << cached<return_two>() << '\n';
}
Which prints: 哪些打印:
Calling 1
1
Calling 2
2
1
2
1
2
Everything looks good to me. 一切对我来说都很好。
However, Netbeans underlines f
in lines 11 and 13, complaining "Unable to resolve identifier f" as an error. 但是,Netbeans在第11行和第13行中在
f
下划线,抱怨“无法解析标识符f”为错误。 I can click the "Run Project (F6)" button and the program happily compiles and runs. 我可以单击“运行项目(F6)”按钮,该程序愉快地编译并运行。
Why does Netbeans complain about my template parameter? 为什么Netbeans抱怨我的模板参数?
For bonus points, the set of functions I have take no parameters, but some return int
, and some return bool
. 为了获得加分,我没有参数的函数集,但是有些返回
int
,有些返回bool
。 I was just going to copy-paste my template, one for each function return type. 我只是要复制粘贴我的模板,每个函数返回类型一个。 Is there a single template I can use to handle both return types?
我可以使用一个模板来处理两种返回类型吗?
You can insert a typedef
to get rid of the red line: 您可以插入
typedef
来消除红线:
typedef int(*f);
template <int(*f)()> // line 11
int cached() {
static int x = f(); // line 13
return x;
}
To get it working with other types: 要使其与其他类型一起使用:
typedef int(*f); // Keeps warning away - again
template <class T, T(*f)()> // line 11
T cached() {
static T x = f(); // line 13
return x;
}
You have to add another template-argument though. 但是,您必须添加另一个模板参数。
Examples: 例子:
bool return_bool()
{
std::cout << "Calling bool\n";
return true;
}
float return_float()
{
std::cout << "Calling float\n";
return 0.31f;
}
std::string return_string()
{
std::cout << "Calling std::string\n";
return "abc";
}
// ...
std::cout << cached<int, return_one>() << '\n';
std::cout << cached<int, return_two>() << '\n';
std::cout << cached<int, return_one>() << '\n';
std::cout << cached<int, return_two>() << '\n';
std::cout << cached<int, return_one>() << '\n';
std::cout << cached<int, return_two>() << '\n';
std::cout << cached<bool, return_bool>() << '\n';
std::cout << cached<bool, return_bool>() << '\n';
std::cout << cached<float, return_float>() << '\n';
std::cout << cached<float, return_float>() << '\n';
std::cout << cached<std::string, return_string>() << '\n';
std::cout << cached<std::string, return_string>() << '\n';
Output: 输出:
Calling one
1
Calling two
2
1
2
1
2
Calling bool
1
1
Calling float
0.31
0.31
Calling std::string
abc
abc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.