nonmember_container_access.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2019 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HISTOGRAM_DETAIL_NONMEMBER_CONTAINER_ACCESS_HPP
  7. #define BOOST_HISTOGRAM_DETAIL_NONMEMBER_CONTAINER_ACCESS_HPP
  8. #if __cpp_lib_nonmember_container_access >= 201411
  9. #include <iterator>
  10. namespace boost {
  11. namespace histogram {
  12. namespace detail {
  13. using std::data;
  14. using std::size;
  15. } // namespace detail
  16. } // namespace histogram
  17. } // namespace boost
  18. #else // std implementations are not available
  19. #include <initializer_list>
  20. #include <type_traits>
  21. namespace boost {
  22. namespace histogram {
  23. namespace detail {
  24. template <class C>
  25. constexpr auto data(C& c) -> decltype(c.data()) {
  26. return c.data();
  27. }
  28. template <class C>
  29. constexpr auto data(const C& c) -> decltype(c.data()) {
  30. return c.data();
  31. }
  32. template <class T, std::size_t N>
  33. constexpr T* data(T (&array)[N]) noexcept {
  34. return array;
  35. }
  36. template <class E>
  37. constexpr const E* data(std::initializer_list<E> il) noexcept {
  38. return il.begin();
  39. }
  40. template <class C>
  41. constexpr auto size(const C& c) -> decltype(c.size()) {
  42. return c.size();
  43. }
  44. template <class T, std::size_t N>
  45. constexpr std::size_t size(const T (&)[N]) noexcept {
  46. return N;
  47. }
  48. } // namespace detail
  49. } // namespace histogram
  50. } // namespace boost
  51. #endif
  52. #endif // BOOST_HISTOGRAM_DETAIL_NONMEMBER_CONTAINER_ACCESS_HPP