2 * Copyright 2016 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 /// move the value out (sugar for std::move(*moveWrapper))
58 T&& move() { return std::move(value); }
60 // If you want these you're probably doing it wrong, though they'd be
61 // easy enough to implement
62 MoveWrapper& operator=(MoveWrapper const&) = delete;
63 MoveWrapper& operator=(MoveWrapper&&) = delete;
69 /// Make a MoveWrapper from the argument. Because the name "makeMoveWrapper"
70 /// is already quite transparent in its intent, this will work for lvalues as
71 /// if you had wrapped them in std::move.
72 template <class T, class T0 = typename std::remove_reference<T>::type>
73 MoveWrapper<T0> makeMoveWrapper(T&& t) {
74 return MoveWrapper<T0>(std::forward<T0>(t));