2 * Copyright 2017 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.
19 #include <type_traits>
27 * Usable when you have a function with two overloads:
30 * void something(MyData&&);
31 * void something(const MyData&);
33 * Where the purpose is to make copies and moves explicit without having to
34 * spell out the full type names - in this case, for copies, to invoke copy
37 * When the caller wants to pass a copy of an lvalue, the caller may:
41 * something(folly::copy(data)); // explicit copy
42 * something(std::move(data)); // explicit move
43 * something(data); // const& - neither move nor copy
46 * Note: If passed an rvalue, invokes the move-ctor, not the copy-ctor. This
47 * can be used to to force a move, where just using std::move would not:
49 * std::copy(std::move(data)); // force-move, not just a cast to &&
51 * Note: The following text appears in the standard:
53 * > In several places in this Clause the operation //DECAY_COPY(x)// is used.
54 * > All such uses mean call the function `decay_copy(x)` and use the result,
55 * > where `decay_copy` is defined as follows:
57 * > template <class T> decay_t<T> decay_copy(T&& v)
58 * > { return std::forward<T>(v); }
60 * > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf
61 * > 30.2.6 `decay_copy` [thread.decaycopy].
63 * We mimic it, with a `noexcept` specifier for good measure.
67 constexpr typename std::decay<T>::type copy(T&& value) noexcept(
68 noexcept(typename std::decay<T>::type(std::forward<T>(value)))) {
69 return std::forward<T>(value);
73 * A simple helper for getting a constant reference to an object.
77 * std::vector<int> v{1,2,3};
78 * // The following two lines are equivalent:
79 * auto a = const_cast<const std::vector<int>&>(v).begin();
80 * auto b = folly::as_const(v).begin();
82 * Like C++17's std::as_const. See http://wg21.link/p0007
84 #if __cpp_lib_as_const || _MSC_VER
86 /* using override */ using std::as_const
91 constexpr T const& as_const(T& t) noexcept {
96 void as_const(T const&&) = delete;