Lua exception handling test
[folly.git] / folly / Launder.h
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <folly/Portability.h>
20
21 namespace folly {
22
23 #ifdef __GNUC__
24 #ifdef __has_builtin
25 #if __has_builtin(__builtin_launder)
26 #define FOLLY_USE_BUILTIN_LAUNDER 1
27 #endif
28 #endif
29 #endif
30
31 /**
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.
34  */
35 template <typename T>
36 FOLLY_NODISCARD inline T* launder(T* in) noexcept {
37 #ifdef __GNUC__
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);
41 #else
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));
45   return in;
46 #endif
47 #else
48   static_assert(
49       false, "folly::launder is not implemented for this environment");
50 #endif
51 }
52
53 #ifdef FOLLY_USE_BUILTIN_LAUNDER
54 #undef FOLLY_USE_BUILTIN_LAUNDER
55 #endif
56
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;
64 } // namespace folly