VCCC  2024.05
VisualCamp Common C++ library
at.hpp
Go to the documentation of this file.
1 # /*
2 # * Created by YongGyu Lee on 2020/12/08.
3 # */
4 #
5 # ifndef VCCC_TYPE_SUPPORT_AT_HPP
6 # define VCCC_TYPE_SUPPORT_AT_HPP
7 #
11 # include "vccc/type_traits.hpp"
12 #
13 # /*
14 # * Note
15 # * vccc::at is implemented in each class' headers
16 # */
17 
18 namespace vccc {
19 
45 template<std::size_t I, typename T, std::enable_if_t<is_tuple_like<std::decay_t<T>>::value, int> = 0>
46 constexpr decltype(auto) at(T&& t) noexcept {
47  return std::get<I>(std::forward<T>(t));
48 }
49 
53 template<std::size_t i, typename C, typename T>
54 constexpr decltype(auto) at(T&& t) {
55  return cast<C>(at<i>(std::forward<T>(t)));
56 }
57 
58 
62 template<std::size_t i, std::size_t j, typename C, typename T>
63 constexpr decltype(auto) at(T&& t) {
64  return cast<C>(at<i, j>(std::forward<T>(t)));
65 }
66 
68 
69 
70 namespace internal {
71 
72 //TODO: change tuple element to use reference_wrapper
73 template<typename ...Ts>
74 class bind_obj {
75  public:
76  template<typename ...Args>
77  constexpr bind_obj(Args&&... args) : tup(std::forward<Args>(args)...) {}
78 
79  template<typename T>
80  constexpr bind_obj& operator = (T&& type)
81  {
82  bind_impl(std::forward<T>(type), std::index_sequence_for<Ts...>{});
83  return *this;
84  }
85 
86  private:
87  template<typename T, std::size_t ...I>
88  constexpr void bind_impl(T&& type, std::index_sequence<I...>)
89  {
90  int dummy[sizeof...(I)] = {
91  (at<I>(tup) = at<I>(std::forward<T>(type)), 0)...
92  };
93  }
94 
95 
96  std::tuple<Ts...> tup;
97 };
98 
99 } // namespace internal
100 
115 template<typename ...Args>
116 constexpr inline internal::bind_obj<Args...> bind_at(Args&&... args) {
117  return internal::bind_obj<Args...>(std::forward<Args>(args)...);
118 }
119 
121 
122 } // namespace vccc
123 
124 # endif // VCCC_TYPE_SUPPORT_AT_HPP
constexpr decltype(auto) at(T &&t) noexcept
Return i-th element of tuple-like object.
Definition: at.hpp:46
constexpr internal::bind_obj< Args... > bind_at(Args &&... args)
call at<...>(...) to args...
Definition: at.hpp:116
Definition: matrix.hpp:495
Definition: directory.h:12