• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2015 Paul Fultz II
3     static_const_var.h
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 
8 #ifndef BOOST_HOF_GUARD_STATIC_CONST_H
9 #define BOOST_HOF_GUARD_STATIC_CONST_H
10 
11 #include <boost/hof/detail/intrinsics.hpp>
12 
13 namespace boost { namespace hof { namespace detail {
14 
15 template<class T>
16 struct static_const_storage
17 {
18     static constexpr T value = T();
19 };
20 
21 template<class T>
22 constexpr T static_const_storage<T>::value;
23 
24 struct static_const_var_factory
25 {
static_const_var_factoryboost::hof::detail::static_const_var_factory26     constexpr static_const_var_factory()
27     {}
28 
29     template<class T>
operator =boost::hof::detail::static_const_var_factory30     constexpr const T& operator=(const T&) const
31     {
32         static_assert(BOOST_HOF_IS_DEFAULT_CONSTRUCTIBLE(T), "Static const variable must be default constructible");
33         return static_const_storage<T>::value;
34     }
35 };
36 }
37 
38 template<class T>
static_const_var()39 constexpr const T& static_const_var()
40 {
41     return detail::static_const_storage<T>::value;
42 }
43 
44 
45 }} // namespace boost::hof
46 
47 #if BOOST_HOF_HAS_RELAXED_CONSTEXPR || defined(_MSC_VER)
48 #define BOOST_HOF_STATIC_CONSTEXPR const constexpr
49 #else
50 #define BOOST_HOF_STATIC_CONSTEXPR static constexpr
51 #endif
52 
53 #if defined(__GNUC__) && !defined (__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7
54 #define BOOST_HOF_STATIC_AUTO_REF extern __attribute__((weak)) constexpr auto
55 #else
56 #define BOOST_HOF_STATIC_AUTO_REF static constexpr auto&
57 #endif
58 
59 // On gcc 4.6 use weak variables
60 #if defined(__GNUC__) && !defined (__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7
61 #define BOOST_HOF_STATIC_CONST_VAR(name) extern __attribute__((weak)) constexpr auto name
62 #else
63 #define BOOST_HOF_STATIC_CONST_VAR(name) static constexpr auto& name = boost::hof::detail::static_const_var_factory()
64 #endif
65 
66 #define BOOST_HOF_DECLARE_STATIC_VAR(name, ...) BOOST_HOF_STATIC_CONST_VAR(name) = __VA_ARGS__{}
67 
68 #endif
69