1 | // (C) Copyright John Maddock 2015. |
2 | // Use, modification and distribution are subject to the |
3 | // Boost Software License, Version 1.0. (See accompanying file |
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | |
6 | #ifndef BOOST_TYPE_TRAITS_INTEGRAL_CONSTANT_HPP |
7 | #define BOOST_TYPE_TRAITS_INTEGRAL_CONSTANT_HPP |
8 | |
9 | #include <boost/config.hpp> |
10 | #include <boost/detail/workaround.hpp> |
11 | |
12 | #if (BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1400)) \ |
13 | || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x610)) \ |
14 | || BOOST_WORKAROUND(__DMC__, BOOST_TESTED_AT(0x840)) \ |
15 | || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3202)) \ |
16 | || BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, BOOST_TESTED_AT(810)) )\ |
17 | || defined(BOOST_MPL_CFG_NO_ADL_BARRIER_NAMESPACE) |
18 | |
19 | |
20 | namespace boost{ |
21 | namespace mpl |
22 | { |
23 | template <bool B> struct bool_; |
24 | template <class I, I val> struct integral_c; |
25 | struct integral_c_tag; |
26 | } |
27 | } |
28 | |
29 | #else |
30 | |
31 | namespace mpl_{ |
32 | |
33 | template <bool B> struct bool_; |
34 | template <class I, I val> struct integral_c; |
35 | struct integral_c_tag; |
36 | } |
37 | |
38 | namespace boost |
39 | { |
40 | namespace mpl |
41 | { |
42 | using ::mpl_::bool_; |
43 | using ::mpl_::integral_c; |
44 | using ::mpl_::integral_c_tag; |
45 | } |
46 | } |
47 | |
48 | #endif |
49 | |
50 | namespace boost{ |
51 | |
52 | template <class T, T val> |
53 | struct integral_constant |
54 | { |
55 | typedef mpl::integral_c_tag tag; |
56 | typedef T value_type; |
57 | typedef integral_constant<T, val> type; |
58 | static const T value = val; |
59 | // |
60 | // This helper function is just to disable type-punning |
61 | // warnings from GCC: |
62 | // |
63 | template <class U> |
64 | static U& dereference(U* p) { return *p; } |
65 | |
66 | operator const mpl::integral_c<T, val>& ()const |
67 | { |
68 | static const char data[sizeof(long)] = { 0 }; |
69 | return dereference(reinterpret_cast<const mpl::integral_c<T, val>*>(&data)); |
70 | } |
71 | BOOST_CONSTEXPR operator T()const { return val; } |
72 | }; |
73 | |
74 | template <class T, T val> |
75 | T const integral_constant<T, val>::value; |
76 | |
77 | template <bool val> |
78 | struct integral_constant<bool, val> |
79 | { |
80 | typedef mpl::integral_c_tag tag; |
81 | typedef bool value_type; |
82 | typedef integral_constant<bool, val> type; |
83 | static const bool value = val; |
84 | // |
85 | // This helper function is just to disable type-punning |
86 | // warnings from GCC: |
87 | // |
88 | template <class T> |
89 | static T& dereference(T* p) { return *p; } |
90 | |
91 | operator const mpl::bool_<val>& ()const |
92 | { |
93 | static const char data = 0; |
94 | return dereference(reinterpret_cast<const mpl::bool_<val>*>(&data)); |
95 | } |
96 | BOOST_CONSTEXPR operator bool()const { return val; } |
97 | }; |
98 | |
99 | template <bool val> |
100 | bool const integral_constant<bool, val>::value; |
101 | |
102 | typedef integral_constant<bool, true> true_type; |
103 | typedef integral_constant<bool, false> false_type; |
104 | |
105 | } |
106 | |
107 | #endif |
108 | |