简体   繁体   English

移至Lambda Capture C ++ 11

[英]Move to lambda capture C++11

In my code I'm using Item 32 advice from Scott Meyers Effective Modern C++, where he explains how to move into the capture in C++11. 在我的代码中,我使用了Scott Meyers Effective Modern C ++的Item 32建议,他在其中解释了如何进入C ++ 11的捕获。 The sample code works well. 示例代码运行良好。

class Some
{
public:
    void foo()
    {
        std::string s = "String";
        std::function<void()> lambda = std::bind([this](std::string& s) {
            bar(std::move(s));
        }, std::move(s));

        call(std::move(lambda));
    }

    void bar(std::string)
    {
        // Some code
    }

    void call(std::function<void()> func)
    {
        func();
    }
};

int main()
{
    Some().foo();
}

Then I tried to use move in the capture in more difficult way with parameters, but it doesn't work, there are some compilation errors. 然后,我尝试以更困难的方式对参数使用捕获中的move,但是它不起作用,存在一些编译错误。 Please, help me to fix it. 请帮我修复它。 Code with error below. 错误代码如下。 I've played with it, but couldn't find a solution. 我已经玩过,但是找不到解决方案。 Is it possible to do it? 有可能做到吗?

class Some
{
public:
    void foo()
    {
        std::string stringToMove = "String";

        std::function<void(std::string, int, int)> lambda =
            std::bind([this](std::string s, int i1, int i2, std::string& stringToMove) {
            bar(std::move(stringToMove), i1, i2);
        }, std::move(stringToMove));

        call(std::move(lambda));
    }

    void bar(std::string, int, int)
    {
        // Some code
    }

    void call(std::function<void(std::string, int, int)> func)
    {
        func(std::string(), 5, 5);
    }
};

int main()
{
    Some().foo();
}

Errors: 错误:

Severity Code Description Project File Line Suppression State Error C2672 'std::invoke': no matching overloaded function found drafts c:\\program files (x86)\\microsoft visual studio 14.0\\vc\\include\\type_traits 1468 严重性代码说明项目文件行抑制状态错误C2672'std :: invoke':找不到匹配的重载函数草稿c:\\ program files(x86)\\ microsoft visual studio 14.0 \\ vc \\ include \\ type_traits 1468

Severity Code Description Project File Line Suppression State Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)' drafts c:\\program files (x86)\\microsoft visual studio 14.0\\vc\\include\\type_traits 1468 严重性代码说明项目文件行抑制状态错误C2893无法专门化功能模板'unknown-type std :: invoke(_Callable &&,_ Types && ...)'草稿c:\\程序文件(x86)\\ Microsoft Visual Studio 14.0 \\ vc \\ include \\ type_traits 1468

std::bind requires of you to specify all the parameters. std::bind要求您指定所有参数。 So those which should be passed to the resulting function object, need to be placeholders. 因此,应该传递给结果函数对象的那些对象必须是占位符。

std::function<void(std::string, int, int)> lambda =
            std::bind([this](std::string s, int i1, int i2, std::string& stringToMove) {
            bar(std::move(stringToMove), i1, i2);
        }, _1, _2, _3, std::move(stringToMove));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM