简体   繁体   English

工作函数指针模板给Netbeans错误

[英]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.

相关问题 将“ this”传递到功能模板中,出现错误 - Pass 'this' into function template, gives error 模板类中的函数指针的成员给出错误:当后跟'::'时必须是类或命名空间 - Member to function pointer inside template class gives error: must be a class or namespace when followed by '::' 模板功能给出“呼叫没有匹配功能”错误 - Template function gives “no matching function for call” error 指向模板方法的指针给出 <unresolved overloaded function type> ) - Pointer to template method gives <unresolved overloaded function type>) 模板推导不适用于功能指针参考 - Template deduction not working for function pointer reference 声明指向模板函数的指针数组会导致编译错误 - Declaring Array of pointer to template functions gives compilation error 如果 constexpr 给出错误,则在 false 中实例化模板函数 - Instantiating template function in a false if constexpr gives error 重新分配指针给出错误 - reassign pointer gives error 使用非捕获lambda作为可变参数模板函数的函数指针参数给出“无匹配函数调用” - using non-capturing lambda as function pointer argument to a variadic template function gives “no matching function call” QT在声明函数指针时给出未定义的引用错误 - QT gives Undefined Reference Error when declaring function pointer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM