相关疑难解决方法(0)

当涉及std :: function或lambda函数时,C++ 11不推断类型

当我定义这个功能时,

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)

c++ lambda templates c++11

54
推荐指数
3
解决办法
1万
查看次数

使用带有lambda的decltype和std :: function

这有效......

auto x = 4;
typedef decltype(x) x_t;
x_t y = 5;
Run Code Online (Sandbox Code Playgroud)

......为什么不呢?

int j = 4;  
auto func = [&] (int i) { cout << "Hello: i=" << i << "  j=" << j << endl;};
typedef decltype(func) lambda_t;
lambda_t func2 = [&] (int i) { cout << "Bye: i=" << i << "  j=" << j << endl;};
Run Code Online (Sandbox Code Playgroud)

...我将如何lambda_t使用std :: function手动声明?

c++ lambda function decltype c++11

9
推荐指数
2
解决办法
3411
查看次数

如何使函数<>参数的C++ 11函数自动接受lambdas

C++ 11同时具有lambda和std :: function <>,但不幸的是,它们有不同的类型.一个结果是,人们无法直接在高阶函数中使用lambda,例如lisp中的map.例如,在以下代码中

 #include <vector>
 #include <functional>
 using namespace std;

 template <typename A,typename B> 
 vector<B> map(std::function<B (A)> f, vector<A> arr) {
       vector<B> res;
       for (int i=0;i<arr.size();i++) res.push_back(f(arr[i]));
       return res;
}

int main () {
    vector<int> a = {1,2,3};
    map([](int x) -> int { return x;},a); //not OK

    auto id_l = [](int x) -> int { return x;};
    map(id_l,a); //not OK;

    function<int (int)> id_f = id_l;
    map(id_f,a); //OK
return 0;
}
Run Code Online (Sandbox Code Playgroud)

,直接使用lambda作为main()的第2行将无法正常工作.g++ -std=c++11 testfunc.cpp返回`... testfunc.cpp:14:37:注意:'main():: __ lambda0'不是从'std …

c++ lambda c++11

7
推荐指数
3
解决办法
1525
查看次数

带有函数指针的c ++模板回调

我想知道是否有可能实现这样的事情,而不会抛出错误:

#include <iostream>

template<typename T>
T Sum(T _arg, T (*callbackFunction)(T))
{
    T result = (*callbackFunction)(_arg);
    return result;
}

template<typename T>
T Callback(T _arg)
{
    std::cout << "Callback is called" << std::endl;
    return _arg;
}

int main()
{
    std::cout << Sum(10.2f, Callback);

    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

cannot use function template 'T Callback(T)' as a function argument
could not deduce template argument for 'T' from 'float'
Run Code Online (Sandbox Code Playgroud)

c++

7
推荐指数
2
解决办法
962
查看次数

接受 lambda 的可变参数模板函数

我试图理解我在下面的代码中遇到的编译器错误。我有一个可变参数模板函数,它接受具有指定类型的 lambda,并尝试调用该函数会导致模板由于不匹配而未被视为有效候选者。

#include <functional>

template<typename ... ResultTypes>
void executeWithResultHandler(std::function<void (ResultTypes...)> lambda)
{
}

int main(int argc, char **argv)
{
    executeWithResultHandler<int>([] (int arg) {
    });
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这会导致以下错误:

$ c++ -std=c++11 reduction.cpp 
reduction.cpp:10:5: error: no matching function for call to 'executeWithResultHandler'
    executeWithResultHandler<int>([] (int arg) {
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reduction.cpp:4:6: note: candidate template ignored: could not match 'function<void (int, type-parameter-0-0...)>' against
      '<lambda at reduction.cpp:10:35>'
void executeWithResultHandler(std::function<void (ResultTypes...)> lambda)
     ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

如果我将声明更改为非可变参数:

template<typename ResultType>
void executeWithResultHandler(std::function<void (ResultType)> lambda)
{
}
Run Code Online (Sandbox Code Playgroud)

那么它适用于上面的玩具示例,但对于真正的问题,我需要任意参数。我在这里遗漏了什么,或者用另一种方式来完成这个?

编辑:这被错误地标记为重复,我相信 …

c++ lambda templates variadic-templates c++11

4
推荐指数
1
解决办法
1420
查看次数

无法隐式初始化std :: function

我写了这个仿函数来执行操作(&&):

// unary functor; performs '&&'
template <typename T>
struct AND
{
    function<bool (const T&)> x;
    function<bool (const T&)> y;

    AND(function<bool (const T&)> xx, function<bool (const T&)> yy) 
             : x(xx), y(yy) {}
    bool operator() ( const T &arg ) { return x(arg) && y(arg); }
};

// helper
template <typename T>
AND<T> And(function<bool (const T&)> xx, function<bool (const T&)> yy)
{
    return AND<T>(xx,yy);
}
Run Code Online (Sandbox Code Playgroud)

注意它的构造函数参数类型: function<bool (const T&)>.

现在,我试图以各种方式(内部big_odd_exists())实例化它:

int is_odd(int n) …
Run Code Online (Sandbox Code Playgroud)

c++ stl c++11

3
推荐指数
1
解决办法
410
查看次数

标签 统计

c++ ×6

c++11 ×5

lambda ×4

templates ×2

decltype ×1

function ×1

stl ×1

variadic-templates ×1