当我定义这个功能时,
template<class A>
set<A> test(const set<A>& input) {
return input;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用test(mySet)代码中的其他地方调用它,而无需显式定义模板类型.但是,当我使用以下功能时:
template<class A>
set<A> filter(const set<A>& input,function<bool(A)> compare) {
set<A> ret;
for(auto it = input.begin(); it != input.end(); it++) {
if(compare(*it)) {
ret.insert(*it);
}
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
当我使用此函数调用时filter(mySet,[](int i) { return i%2==0; });
,出现以下错误:
错误:没有匹配函数来调用'filter(std :: set&,main()::)'
但是,所有这些版本都可以工作:
std::function<bool(int)> func = [](int i) { return i%2 ==0; };
set<int> myNewSet = filter(mySet,func);
set<int> myNewSet = filter<int>(mySet,[](int i) { return i%2==0; });
set<int> myNewSet …Run Code Online (Sandbox Code Playgroud) 在模板化成员函数中使用std :: function时出现编译错误,以下代码是一个简单示例:
#include <functional>
#include <memory>
using std::function;
using std::bind;
using std::shared_ptr;
class Test {
public:
template <typename T>
void setCallback(function<void (T, int)> cb);
};
template <typename T>
void Test::setCallback(function<void (T, int)> cb)
{
// do nothing
}
class TestA {
public:
void testa(int a, int b) { }
};
int main()
{
TestA testA;
Test test;
test.setCallback(bind(&TestA::testa, &testA, std::placeholders::_1, std::placeholders::_2));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并带来以下编译错误:
testtemplate.cpp:在函数'int main()'中:
testtemplate.cpp:29:92:错误:没有匹配的函数调用"测试:: setCallback(STD :: _ Bind_helper)(INT,INT),种皮,常量性病:: _占位符<1>&,常量性病:: _占位符<2>&> ::类型)"
testtemplate.cpp:29:92:注意:候选者是:testtemplate.cpp:10:7:注意:模板void test :: setCallback(std …
我正在学习C++,我正在尝试实现一个二进制搜索函数,它找到谓词所在的第一个元素.函数的第一个参数是向量,第二个参数是一个计算给定元素的谓词的函数.二进制搜索功能如下所示:
template <typename T> int binsearch(const std::vector<T> &ts, bool (*predicate)(T)) {
...
}
Run Code Online (Sandbox Code Playgroud)
如果像这样使用,这可以按预期工作:
bool gte(int x) {
return x >= 5;
}
int main(int argc, char** argv) {
std::vector<int> a = {1, 2, 3};
binsearch(a, gte);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用lambda函数作为谓词,我会收到编译器错误:
search-for-a-range.cpp:20:5: error: no matching function for call to 'binsearch'
binsearch(a, [](int e) -> bool { return e >= 5; });
^~~~~~~~~
search-for-a-range.cpp:6:27: note: candidate template ignored: could not match 'bool (*)(T)' against '(lambda at
search-for-a-range.cpp:20:18)'
template <typename T> int …Run Code Online (Sandbox Code Playgroud) 考虑这个模板功能:
template<typename ReturnT>
ReturnT foo(const std::function<ReturnT ()>& fun)
{
return fun();
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器不能ReturnT从传递的调用签名中推断出来?
bool bar() { /* ... */ }
foo<bool>(bar); // works
foo(bar); // error: no matching function call
Run Code Online (Sandbox Code Playgroud) 我从这里获得的问题的依据是: 无法从lambda函数推断出模板参数std :: function 。此线程中的问题是:为什么此代码无法将lambda传递给函数:
#include <iostream>
#include <functional>
template <typename T>
void call(std::function<void(T)> f, T v)
{
f(v);
}
int main(int argc, char const *argv[])
{
auto foo = [](int i) {
std::cout << i << std::endl;
};
call(foo, 1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该线程的答案是,因为lambda不是a std::function。但是为什么这段代码会编译:
#include <iostream>
#include <functional>
template <typename T>
void call(std::function<void(T)> f, T v)
{
f(v);
}
int main(int argc, char const *argv[])
{
auto foo = [](int i) {
std::cout << i << std::endl; …Run Code Online (Sandbox Code Playgroud)