nonmember_container_access.hpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #include <initializer_list>
  9. #include <type_traits>
  10. #include <valarray>
  11. namespace boost {
  12. namespace histogram {
  13. namespace detail {
  14. template <class C>
  15. constexpr auto data(C& c) -> decltype(c.data()) {
  16. return c.data();
  17. }
  18. template <class C>
  19. constexpr auto data(const C& c) -> decltype(c.data()) {
  20. return c.data();
  21. }
  22. template <class T, std::size_t N>
  23. constexpr T* data(T (&array)[N]) noexcept {
  24. return array;
  25. }
  26. template <class E>
  27. constexpr const E* data(std::initializer_list<E> il) noexcept {
  28. return il.begin();
  29. }
  30. template <class E>
  31. constexpr const E* data(const std::valarray<E>& v) noexcept {
  32. return std::begin(v);
  33. }
  34. template <class E>
  35. constexpr E* data(std::valarray<E>& v) noexcept {
  36. return std::begin(v);
  37. }
  38. template <class C>
  39. constexpr auto size(const C& c) -> decltype(c.size()) {
  40. return c.size();
  41. }
  42. template <class T, std::size_t N>
  43. constexpr std::size_t size(const T (&)[N]) noexcept {
  44. return N;
  45. }
  46. } // namespace detail
  47. } // namespace histogram
  48. } // namespace boost
  49. #endif // BOOST_HISTOGRAM_DETAIL_NONMEMBER_CONTAINER_ACCESS_HPP