Skip to content

Commit 9577b08

Browse files
committed
Add mstd_new for mstd::launder
Increasingly clever compilers can hit funny aliasing problems with object stores like mbed::Callback. Add access to the C++17 launder facility.
1 parent 33d9abe commit 9577b08

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

platform/cxxsupport/mstd_new

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#ifndef MSTD_NEW_
18+
#define MSTD_NEW_
19+
20+
/* <mstd_new>
21+
*
22+
* - includes toolchain's <new>
23+
* - For all toolchains, C++17 backports:
24+
* - mstd::launder
25+
*/
26+
27+
#include <new>
28+
#if __cpp_lib_launder < 201606
29+
#include <type_traits>
30+
#endif
31+
32+
namespace mstd
33+
{
34+
using std::nothrow_t;
35+
using std::nothrow;
36+
using std::new_handler;
37+
using std::set_new_handler;
38+
39+
#if __cpp_lib_launder >= 201606
40+
using std::launder;
41+
#else
42+
template <typename T>
43+
constexpr T *launder(T *p) noexcept
44+
{
45+
static_assert(!std::is_function<T>::value && !std::is_void<T>::value, "Can only launder complete object types");
46+
#if defined __clang__ || __GNUC__ >= 9
47+
return __builtin_launder(p);
48+
#else
49+
return p;
50+
#endif
51+
}
52+
#endif
53+
54+
} // namespace mstd
55+
56+
#endif // MSTD_NEW_

0 commit comments

Comments
 (0)