[英]std::function and error: no matching function for call to
I am calling a template based function which shares a type between a function and structure. 我正在调用一个基于模板的函数,它在函数和结构之间共享一个类型。 What is wrong with this code? 这段代码有什么问题? why do I receive error when I compile it? 编译时为什么会收到错误?
test.cpp TEST.CPP
#include <functional>
#include <iostream>
template<typename T>
struct mystruct
{
T variable;
};
int myfunc(int x)
{
return 2*x;
}
template<typename T>
T calculate(
mystruct<T> custom_struct,
std::function<T(T)> custom_func)
{
return custom_func(custom_struct.variable);
}
int main()
{
mystruct<int> A;
A.variable=6;
std::cout<<calculate(A,myfunc)<<std::endl;
return 0;
}
Compiler results: 编译结果:
test.cpp:25:31: error: no matching function for call to ‘calculate(mystruct<int>&, int (&)(int))’
std::cout<<calculate(A,myfunc)<<std::endl;
^
There is no reason to use the std::function
wrapper. 没有理由使用std::function
包装器。 Instead use a general template parameter F
而是使用通用模板参数F
template<typename T, class F>
T calculate(
mystruct<T> custom_struct,
F custom_func)
{
return custom_func(custom_struct.variable);
}
Note that you also forgot to access the variable
member at the call site. 请注意,您也忘记了在呼叫站点访问variable
成员。 Since you are doing generic programming here, you also want the return type to be equal to T
, or even auto
(C++14, for C++11 you want probably use decltype
but that is too much repetition). 既然你在这里进行泛型编程,你也希望返回类型等于T
,甚至是auto
(C ++ 14,对于C ++ 11你可能想要使用decltype
但重复次数太多了)。
Your code is a bit messy, but theres always a solution. 你的代码有点乱,但总是一个解决方案。
#include <functional>
#include <iostream>
template<typename T>
struct mystruct
{
T variable;
};
const int myfunc(const int & x)
{
return 2*x;
}
template<typename T>
T calculate(
mystruct<T> custom_struct,
std::function<T(T)> custom_func)
{
return custom_func(custom_struct.variable);
}
int main()
{
mystruct<int> A;
A.variable=6;
std::cout<<calculate<int>(A,myfunc)<<std::endl;
return 0;
}
There was just a problem with return custom_func(custom_struct)
, you've to pass the variable
member from that struct and add calculate<int>
instead of calculate
. return custom_func(custom_struct)
只是一个问题,你要从该结构传递variable
成员并添加calculate<int>
而不是calculate
。
You can try/test the new code here: http://cpp.sh/33cpn 您可以在此处尝试/测试新代码: http : //cpp.sh/33cpn
template<typename T>
int calculate(
mystruct<T> custom_struct,
std::function<T(T)> custom_func);
The compiler will attempt to deduce T
from std::function<T(T)>
as well as mystruct<T>
, but deduction from the function pointer fails. 编译器将尝试推断T
从std::function<T(T)>
以及mystruct<T>
但扣从函数指针失败。 One solution would be to disable template deduction on std::function<T(T)>
by making it a non-deduced context: 一种解决方案是通过使其成为非推导的上下文来禁用std::function<T(T)>
上的模板推导:
template <typename T> struct identity { using type = T; };
template <typename T> using identity_t = typename identity<T>::type;
template<typename T>
int calculate(
mystruct<T> custom_struct,
identity_t<std::function<T(T)>> custom_func)
{
return custom_func(custom_struct.variable);
}
Although it makes the function signature a bit uglier, you can still have T
deduced, so you can just call calculate(A,myfunc)
rather than calculate<int>(A,myfunc)
. 虽然它使函数签名有点丑陋,但你仍然可以推导出T
,所以你可以调用calculate(A,myfunc)
而不是calculate<int>(A,myfunc)
。
However, in this situation you should use TemplateRex's solution as std::function
comes with a bunch of overhead which you don't actually need unless you're wanting to store it somewhere. 但是,在这种情况下你应该使用TemplateRex的解决方案,因为std::function
附带了一堆你实际上并不需要的开销,除非你想把它存储在某个地方。
You are wrongly passing custom_struct
to custom_func
in calculate()
. 您错误地将custom_struct
传递给calculate()
custom_func
。
Try passing custom_struct.variable
instead: 尝试传递custom_struct.variable
:
template<typename T>
int calculate(
mystruct<T> custom_struct,
std::function<T(T)> custom_func)
{
return custom_func(custom_struct.variable);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.