2 * Copyright 2017-present 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 <folly/Portability.h>
25 #if __has_builtin(__builtin_launder)
26 #define FOLLY_USE_BUILTIN_LAUNDER 1
32 * Approximate backport from C++17 of std::launder. It should be `constexpr`
33 * but that can't be done without specific support from the compiler.
36 FOLLY_NODISCARD inline T* launder(T* in) noexcept {
38 #ifdef FOLLY_USE_BUILTIN_LAUNDER
39 // Newer GCC versions have a builtin for this with no unwanted side-effects
40 return __builtin_launder(in);
42 // This inline assembler block declares that `in` is an input and an output,
43 // so the compiler has to assume that it has been changed inside the block.
44 __asm__("" : "+r"(in));
49 false, "folly::launder is not implemented for this environment");
53 #ifdef FOLLY_USE_BUILTIN_LAUNDER
54 #undef FOLLY_USE_BUILTIN_LAUNDER
57 /* The standard explicitly forbids laundering these */
58 void launder(void*) = delete;
59 void launder(void const*) = delete;
60 void launder(void volatile*) = delete;
61 void launder(void const volatile*) = delete;
62 template <typename T, typename... Args>
63 void launder(T (*)(Args...)) = delete;