static_constant.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2008 Matthias Christian Schabel
  5. // Copyright (C) 2008 Steven Watanabe
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_UNITS_STATIC_CONSTANT_HPP
  11. #define BOOST_UNITS_STATIC_CONSTANT_HPP
  12. #include <boost/units/config.hpp>
  13. #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_UNITS_DOXYGEN)
  14. /// A convenience macro that allows definition of static
  15. /// constants in headers in an ODR-safe way.
  16. # define BOOST_UNITS_STATIC_CONSTANT(name, type) \
  17. template<bool b> \
  18. struct name##_instance_t \
  19. { \
  20. static const type instance; \
  21. }; \
  22. \
  23. namespace \
  24. { \
  25. static const type& name = name##_instance_t<true>::instance; \
  26. } \
  27. \
  28. template<bool b> \
  29. const type name##_instance_t<b>::instance
  30. #else
  31. # define BOOST_UNITS_STATIC_CONSTANT(name, type) \
  32. BOOST_STATIC_CONSTEXPR type name
  33. #endif
  34. /// A convenience macro for static constants with auto
  35. /// type deduction.
  36. #if BOOST_UNITS_HAS_TYPEOF
  37. #if BOOST_UNITS_HAS_BOOST_TYPEOF
  38. #define BOOST_UNITS_AUTO_STATIC_CONSTANT(name, value) \
  39. BOOST_TYPEOF_NESTED_TYPEDEF(name##_nested_t, value) \
  40. BOOST_UNITS_STATIC_CONSTANT(name, name##_nested_t::type) = (value)
  41. #elif BOOST_UNITS_HAS_MWERKS_TYPEOF
  42. #define BOOST_UNITS_AUTO_STATIC_CONSTANT(name, value) \
  43. BOOST_UNITS_STATIC_CONSTANT(name, __typeof__(value)) = (value)
  44. #elif BOOST_UNITS_HAS_GNU_TYPEOF
  45. #define BOOST_UNITS_AUTO_STATIC_CONSTANT(name, value) \
  46. BOOST_UNITS_STATIC_CONSTANT(name, typeof(value)) = (value)
  47. #endif // BOOST_UNITS_HAS_BOOST_TYPEOF
  48. #endif // BOOST_UNITS_HAS_TYPEOF
  49. #endif // BOOST_UNITS_STATIC_CONSTANT_HPP