移动到 lambda 捕获 C++11
Move to lambda capture C++11
在我的代码中,我使用了 Scott Meyers Effective Modern C++ 中的第 32 项建议,他在其中解释了如何在 C++11 中进入捕获。示例代码运行良好。
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();
}
然后我尝试用更难的带参数的方式在捕获中使用移动,但是它不起作用,有一些编译错误。请帮我解决它。下面有错误的代码。我玩过它,但找不到解决方案。可以吗?
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();
}
错误:
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
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
std::bind
要求您指定所有参数。所以那些应该传递给结果函数对象的,需要是占位符。
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));
在我的代码中,我使用了 Scott Meyers Effective Modern C++ 中的第 32 项建议,他在其中解释了如何在 C++11 中进入捕获。示例代码运行良好。
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();
}
然后我尝试用更难的带参数的方式在捕获中使用移动,但是它不起作用,有一些编译错误。请帮我解决它。下面有错误的代码。我玩过它,但找不到解决方案。可以吗?
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();
}
错误:
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
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
std::bind
要求您指定所有参数。所以那些应该传递给结果函数对象的,需要是占位符。
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));