【发布时间】:2017-02-21 12:20:32
【问题描述】:
我正在调用一个基于模板的函数,它在函数和结构之间共享一个类型。这段代码有什么问题?为什么编译的时候会报错?
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;
}
编译结果:
test.cpp:25:31: error: no matching function for call to ‘calculate(mystruct<int>&, int (&)(int))’
std::cout<<calculate(A,myfunc)<<std::endl;
^
【问题讨论】:
标签: c++ c++11 templates std-function