]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | /* |
2 | * Distributed under the Boost Software License, Version 1.0. | |
3 | * (See accompanying file LICENSE_1_0.txt or copy at | |
4 | * http://www.boost.org/LICENSE_1_0.txt) | |
5 | * | |
6 | * Copyright (c) 2018 Andrey Semashev | |
7 | */ | |
8 | /*! | |
9 | * \file atomic/detail/addressof.hpp | |
10 | * | |
11 | * This header defines \c addressof helper function. It is similar to \c boost::addressof but it is more | |
12 | * lightweight and also contains a workaround for some compiler warnings. | |
13 | */ | |
14 | ||
15 | #ifndef BOOST_ATOMIC_DETAIL_ADDRESSOF_HPP_INCLUDED_ | |
16 | #define BOOST_ATOMIC_DETAIL_ADDRESSOF_HPP_INCLUDED_ | |
17 | ||
18 | #include <boost/atomic/detail/config.hpp> | |
20effc67 | 19 | #include <boost/atomic/detail/header.hpp> |
11fdf7f2 TL |
20 | |
21 | #ifdef BOOST_HAS_PRAGMA_ONCE | |
22 | #pragma once | |
23 | #endif | |
24 | ||
25 | // Detection logic is based on boost/core/addressof.hpp | |
26 | #if defined(BOOST_MSVC_FULL_VER) && BOOST_MSVC_FULL_VER >= 190024215 | |
27 | #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF | |
28 | #elif defined(BOOST_GCC) && BOOST_GCC >= 70000 | |
29 | #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF | |
30 | #elif defined(__has_builtin) | |
31 | #if __has_builtin(__builtin_addressof) | |
32 | #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF | |
33 | #endif | |
34 | #endif | |
35 | ||
36 | namespace boost { | |
37 | namespace atomics { | |
38 | namespace detail { | |
39 | ||
40 | template< typename T > | |
1e59de90 TL |
41 | BOOST_FORCEINLINE |
42 | #if defined(BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF) | |
43 | BOOST_CONSTEXPR | |
44 | #endif | |
45 | T* addressof(T& value) BOOST_NOEXCEPT | |
11fdf7f2 TL |
46 | { |
47 | #if defined(BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF) | |
48 | return __builtin_addressof(value); | |
49 | #else | |
50 | // Note: The point of using a local struct as the intermediate type instead of char is to avoid gcc warnings | |
51 | // if T is a const volatile char*: | |
52 | // warning: casting 'const volatile char* const' to 'const volatile char&' does not dereference pointer | |
53 | // The local struct makes sure T is not related to the cast target type. | |
54 | struct opaque_type; | |
55 | return reinterpret_cast< T* >(&const_cast< opaque_type& >(reinterpret_cast< const volatile opaque_type& >(value))); | |
56 | #endif | |
57 | } | |
58 | ||
59 | } // namespace detail | |
60 | } // namespace atomics | |
61 | } // namespace boost | |
62 | ||
20effc67 TL |
63 | #include <boost/atomic/detail/footer.hpp> |
64 | ||
11fdf7f2 | 65 | #endif // BOOST_ATOMIC_DETAIL_ADDRESSOF_HPP_INCLUDED_ |