funcy  1.6.1
constant.h
1 #pragma once
2 
3 #include <type_traits>
4 #include <utility>
5 
6 namespace funcy
7 {
9  template < class Type >
10  struct Constant
11  {
12  constexpr Constant() = default;
13 
14  constexpr Constant( Type&& t_ ) noexcept( std::is_nothrow_move_constructible_v< Type > )
15  : t( std::move( t_ ) )
16  {
17  }
18 
19  constexpr Constant( const Type& t_ ) noexcept(
20  std::is_nothrow_copy_constructible_v< Type > )
21  : t( t_ )
22  {
23  }
24 
26  constexpr const Type& operator()() const noexcept
27  {
28  return t;
29  }
30 
31  private:
32  Type t;
33  };
34 
40  template < class Arg >
41  auto const_ref( const Arg& x ) noexcept( std::is_nothrow_copy_constructible_v< Arg > )
42  {
43  return Constant< const Arg& >( x );
44  }
45 
50  template < class Arg >
51  constexpr auto constant( Arg&& x ) noexcept(
52  (std::is_rvalue_reference_v< Arg > && std::is_nothrow_move_constructible_v< Arg >) ||
53  (std::is_lvalue_reference_v< Arg > && std::is_nothrow_copy_constructible_v< Arg >))
54  {
55  return Constant( std::forward< Arg >( x ) );
56  }
57 } // namespace funcy
auto const_ref(const Arg &x) noexcept(std::is_nothrow_copy_constructible_v< Arg >)
Generate a constant function that stores its argument as constant reference.
Definition: constant.h:41
Main namespace of the funcy library.
constexpr auto constant(Arg &&x) noexcept((std::is_rvalue_reference_v< Arg > &&std::is_nothrow_move_constructible_v< Arg >)||(std::is_lvalue_reference_v< Arg > &&std::is_nothrow_copy_constructible_v< Arg >))
Wrap a constant.
Definition: constant.h:51
Wrap a constant.
Definition: constant.h:10
constexpr const Type & operator()() const noexcept
Function value.
Definition: constant.h:26