As usual, Scott Meyers offers a great explanation of what they are, why you would like to use them, and roughly what rules they follow in Effective Modern C ++ . Paragraph 32: Use the init capture to move objects to close. Here is a brief summary:
++ 11 - , . , , ++ 14 - , init.
init - , .. ( ).
, init :
auto pw = std::make_unique<Widget>();
auto func = [pWidget = std::move(pw)] {
return pWidget->isValidated() && pWidget->isArchived();
};
init ++ 11
:
class IsValAndArch {
public:
using DataType = std::unique_ptr<Widget>;
explicit IsValAndArch(DataType&& ptr) : pw(std::move(ptr)) {}
bool operator()() const { return pw->isValid() && pw->isArchived(); }
private:
DataType pw;
};
auto pw = std::make_unique<Widget>();
auto func = IsValAndArch(pw);
std::bind:
auto pw = std::make_unique<Widget>();
auto func = std::bind( [](const std::unique_ptr<Widget>& pWidget)
{ return pWidget->isValidated() && pWidget->isArchived(); },
std::move(pw) );
, lambda lvalue ( pw lvalue) const ( -, , ββ, const ).
init Anthony Calandra cheatsheet ++, , ( ).