siplasplas
A library for C++ reflection and introspection
identity.hpp
1 #ifndef SIPLASPLAS_UTILITY_IDENTITY_HPP
2 #define SIPLASPLAS_UTILITY_IDENTITY_HPP
3 
4 #include "meta.hpp"
5 #include <utility>
6 
7 namespace cpp
8 {
9 
21 class Identity
22 {
23 public:
24  template<typename T>
25  constexpr auto operator()(T&& value) -> decltype(std::forward<T>(value)) const
26  {
27  return std::forward<T>(value);
28  }
29 
30  template<typename T, typename Function>
31  constexpr auto type(Function callback) -> decltype(callback(meta::identity<T>())) const
32  {
33  return callback(meta::identity<T>());
34  }
35 
36  template<typename T>
37  constexpr auto type() const
38  {
39  return meta::identity<T>();
40  }
41 };
42 
43 inline auto applyIdentity(const Identity& identity)
44 {
45  return [identity](auto&& value)
46  {
47  return identity(std::forward<decltype(value)>(value));
48  };
49 }
50 
51 template<typename T, typename Identity>
52 constexpr auto applyIdentity(const Identity& identity) -> meta::type_t<decltype(identity.template type<T>())>;
53 
54 }
55 
56 #endif // SIPLASPLAS_UTILITY_IDENTITY_HPP
A functor class implementing the identity function.
Definition: identity.hpp:21
Definition: canary_allocator.hpp:7