2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 /** C++11 closures don't support move-in capture. Nor does std::bind.
26 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3610.html
28 "[...] a work-around that should make people's stomach crawl:
29 write a wrapper that performs move-on-copy, much like the deprecated
32 Unlike auto_ptr, this doesn't require a heap allocation.
37 /** If value can be default-constructed, why not?
38 Then we don't have to move it in */
39 MoveWrapper() = default;
43 MoveWrapper(T&& t) : value(std::move(t)) {}
46 MoveWrapper(const MoveWrapper& other) : value(std::move(other.value)) {}
49 MoveWrapper(MoveWrapper&& other) : value(std::move(other.value)) {}
51 const T& operator*() const { return value; }
52 T& operator*() { return value; }
54 const T* operator->() const { return &value; }
55 T* operator->() { return &value; }
57 // If you want these you're probably doing it wrong, though they'd be
58 // easy enough to implement
59 MoveWrapper& operator=(MoveWrapper const&) = delete;
60 MoveWrapper& operator=(MoveWrapper&&) = delete;
67 MoveWrapper<T> makeMoveWrapper(T&& t) {
68 return MoveWrapper<T>(std::forward<T>(t));