CNL  2.0.2 (development)
Compositional Numeric Library
All Classes Namespaces Files Functions Variables Typedefs Macros Pages
from_value.h
1 
2 // Copyright John McFarlane 2018.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file ../LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #if !defined(CNL_IMPL_NUM_TRAITS_FROM_VALUE_H)
8 #define CNL_IMPL_NUM_TRAITS_FROM_VALUE_H
9 
10 #include "../../constant.h"
11 #include "../numbers/set_signedness.h"
12 #include "../type_traits/remove_cvref.h"
13 #include "../used_digits.h"
14 #include "digits.h"
15 #include "set_digits.h"
16 
17 #include <algorithm>
18 #include <concepts>
19 
20 namespace cnl {
21  namespace _impl {
22  template<typename Result, typename Value>
23  struct from_value_simple {
24  [[nodiscard]] constexpr auto operator()(Value const& value) const
25  {
26  return Result{value};
27  }
28  };
29  }
30 
31  template<typename Number, typename Value>
32  struct from_value {
33  void operator()(Value const&) const;
34  };
35 
36  template<std::integral Number, class Value>
37  struct from_value<Number, Value>
38  : _impl::from_value_simple<Value, Value> {
39  };
40 
41  template<std::integral Number, CNL_IMPL_CONSTANT_VALUE_TYPE Value>
42  struct from_value<Number, constant<Value>> {
43  private:
44  using result_type = set_digits_t<
45  numbers::set_signedness_t<Number, true>,
46  std::max(digits_v<int>, _impl::used_digits(Value))>;
47 
48  public:
49  [[nodiscard]] constexpr auto operator()(constant<Value> const& value) const
50  {
51  return result_type(value);
52  }
53  };
54 
55  namespace _impl {
56  template<typename Number, typename Value>
57  [[nodiscard]] constexpr auto from_value(Value const& value)
58  -> decltype(cnl::from_value<Number, remove_cvref_t<Value>>{}(value))
59  {
60  return cnl::from_value<Number, remove_cvref_t<Value>>{}(value);
61  }
62  }
63 
64  template<typename Number, typename Value>
65  using from_value_t = decltype(_impl::from_value<Number>(std::declval<Value>()));
66 }
67 
68 #endif // CNL_IMPL_NUM_TRAITS_FROM_VALUE_H
cnl::set_digits_t
typename set_digits< T, Digits >::type set_digits_t
Alias to set_digits.
Definition: set_digits.h:95
cnl
compositional numeric library
Definition: abort.h:15
std::max
T max(T... args)