exptl
General C++20 library that complements the C++ Standard Library
Public Member Functions
exptl::composable< Fn > Class Template Reference

Wraps around a callable object to provide function composition. More...

Public Member Functions

 composable (Fn fn)
 Creates a composable<Fn> wrapping the callable fn. More...
 
template<typename Fn2 >
auto operator+ (Fn2 rhs) const
 Returns the composition of this composable and rhs. More...
 
template<typename... Args>
decltype(auto) operator() (Args &&...args)
 Calls the wrapped function the provided arguments. More...
 
template<typename... Args>
decltype(auto) operator() (Args &&...args) const
 Calls the wrapped function with the provided arguments. More...
 

Detailed Description

template<typename Fn>
class exptl::composable< Fn >

Wraps around a callable object to provide function composition.

Template Parameters
FnThe callable object type.

Constructor & Destructor Documentation

◆ composable()

template<typename Fn >
exptl::composable< Fn >::composable ( Fn  fn)
inline

Creates a composable<Fn> wrapping the callable fn.

Parameters
fnThe callable object to wrap.
33: fn{fn} {}

Member Function Documentation

◆ operator()() [1/2]

template<typename Fn >
template<typename... Args>
decltype(auto) exptl::composable< Fn >::operator() ( Args &&...  args)
inline

Calls the wrapped function the provided arguments.

Template Parameters
ArgsType of the arguments to pass to the wrapped function.
Parameters
argsThe arguments to pass to the wrapped function.
Returns
The result of calling the wrapped function.
70 {
71 return std::invoke(fn, std::forward<Args>(args)...);
72 }

◆ operator()() [2/2]

template<typename Fn >
template<typename... Args>
decltype(auto) exptl::composable< Fn >::operator() ( Args &&...  args) const
inline

Calls the wrapped function with the provided arguments.

Only allowed when the wrapped callable is equality preserving.

Template Parameters
ArgsType of the arguments to pass to the wrapped function.
Parameters
argsThe arguments to pass to the wrapped function.
Returns
The result of calling the wrapped function.
87 {
88 return std::invoke(fn, std::forward<Args>(args)...);
89 }

◆ operator+()

template<typename Fn >
template<typename Fn2 >
auto exptl::composable< Fn >::operator+ ( Fn2  rhs) const
inline

Returns the composition of this composable and rhs.

Given that fn is the currently wrapped callable, returns a composable that wraps around a callable which invokes rhs(fn(args)) when called with the arguments args.

Template Parameters
Fn2Type of the callable object to compose with.
Parameters
rhsThe callable object to compose with.
48 {
49 auto lambda{
50 [=, fn{fn}](auto &&...args) mutable -> decltype(auto)
51 {
52 return std::invoke(
53 rhs, std::invoke(fn, std::forward<decltype(args)>(args)...));
54 }};
55
56 return composable<decltype(lambda)>{lambda};
57 }
composable(Fn fn)
Creates a composable<Fn> wrapping the callable fn.
Definition: functional.hpp:33