ちなみに,最新の gcc/msvc は次もok
http://melpon.org/wandbox/permlink/Gmqycg5tbA2iE51S
usingiVec = std::vector< int >;
iVec data = iVec{1, 2, 3, 4, 5};
auto fun = [data{std::move(data)}]
{ std::copy(std::begin(data),
std::end(data),
[] () mutable
{ data.clear(): }};
prog.cc:9:19: warning:
direct list initialization of a lambda init-capture will change meaning in a future version of Cla
insert an '=' to avoid a change in behavior [-Wfuture-compat]
auto f = [data{ std::move(data) } ] {
^
clang だと次の messageが出る
ref:
N3681 Auto and Braced-Init-Lists
N3912 Atuto and Braced-Init-Lists, continued
N3922 New Rules for auto deduction from braced-init-list
7.
template< class T>
struct wrap {
mutable T value_;
wrap(T&& v) : value_(std::forward< T >(v)) {}
wrap(wrap const& other) : value_(std::move(other.value_)) {}
};
struct Widget {};
auto pw = wrap< std::unique_ptr< Widget > >{std::make_unique< Widget >()};
auto fun = [pw]{ /* do something */ }
こんな wrapper を書くとか...
ref:N3610: Generic lambda-capture initializers
C++14 なら,初期化キャプチャーでできる
8.
class Widget {
public:
/*... */
bool isValidated() const;
bool isProessed() const;
bool isAchieved() const;
private:
/* ... */
};
auto pw = std::make_unique< Widget >();
/**
* configure *pw
**/
auto func = [pw = std::move(pw)]
{ return pw->isValidated()
&& pw->isAchived();};
/**
* pw is moved to data member pw of closure
**/
こうなる