ugmisc 0.2-76
Miscellaneous C++ header library
Loading...
Searching...
No Matches
static_member_caller< Access, T > Struct Template Reference

#include <ugmisc/member.hpp>

Public Types

template<class... Args>
using matched_type = typename caller<void, types_list, Args...>::type
template<class... Args>
using safe_matched_type

Public Member Functions

template<class... Args>
constexpr decltype(auto) operator() (Args &&...args) const

Static Public Member Functions

template<class... Args>
static constexpr bool is_matched (Args &&...)
template<class... Args>
static constexpr decltype(auto) call (Args &&...args)
template<class F>
static constexpr member_::fallback_caller< static_member_caller, std::remove_reference_t< F > > fallback (F &&functor)
template<class... Args>
static matched_type< Args... > declval_matched (Args &&...) noexcept
template<class... Args>
static matched_type< Args... > declval_matched () noexcept
template<class... Args>
static safe_matched_type< Args... > safe_declval_matched (Args &&...) noexcept
template<class... Args>
static safe_matched_type< Args... > safe_declval_matched () noexcept

Static Public Attributes

template<class... Args>
static constexpr bool is_matched_v

Detailed Description

template<class Access, class... T>
struct static_member_caller< Access, T >

Can find, among a list of types, a static method with a particular name, which accepts certain parameter types, and forward arguments to it.

Also provides the ability to revert to a default value if no suitable call can be made. This can be done in such a way that the default value does not have to be instantiated if it is not needed.

Template Parameters
AccessA type, which may be declared using UGMISC_DECL_MEMBER_ACCESS, which is responsible for knowing the name of the function to call.
TA list of types which may or may not contain the named static method. The first one which is suitable will be selected.

For example, if you want to forward args... to the first of X::foo(), Y::foo(), and Z::foo() which accepts those arguments, you can do this:

UGMISC_DECL_MEMBER_ACCESS(FooCall, foo); // Declares FooCall.
F::call(args...); // Error if X, Y, and Z don't have a static method called "foo".
F().call(args...); // Same as F::call(args...).
F()(args...); // Same as F::call(args...).
#define UGMISC_DECL_MEMBER_ACCESS(TNAME, NAME)
Definition member.hpp:165
Definition member.hpp:593

Default values

Continuing the above example:

F::fallback( f )(args...);
F().fallback( f )(args...); // Same as above.

The first of the following expressions which is valid is what the above expression is equivalent to:

  • X::foo(args...)
  • Y::foo(args...)
  • Z::foo(args...)
  • f(args...)
  • f()

f would typically be a lambda.

If all you want to know is whether the call with those arguments would be valid:

F::is_matched(args...); // Boolean.
F::is_matched_v<ArgTypes...>; // Same value.

You can also find the type whose foo method would be called:

F::matched_type<ArgTypes...>; // Error if none matched.
F::safe_matched_type<ArgTypes...>; // void if none matched.

Member Typedef Documentation

◆ matched_type

template<class Access, class... T>
template<class... Args>
using static_member_caller< Access, T >::matched_type = typename caller<void, types_list, Args...>::type

The type, which is one of the T... template parameter pack, that would be called if call were called with these argument types.

◆ safe_matched_type

template<class Access, class... T>
template<class... Args>
using static_member_caller< Access, T >::safe_matched_type
Initial value:
typename caller<void, types_list, Args...>::safe_type

If is_matched_v<Args...> is true: the type whose named (by Access) method would be called by call<Args...>.

If is_matched_v<Args...> is false: void`.

Member Function Documentation

◆ call()

template<class Access, class... T>
template<class... Args>
constexpr decltype(auto) static_member_caller< Access, T >::call ( Args &&... args)
inlinestaticconstexpr

Forward the arguments, using std::forward, to a static method of one of the types T....

The name of the static method is determined by the Access template parameter.

The first type for which the call would be valid is used.

◆ declval_matched() [1/2]

template<class Access, class... T>
template<class... Args>
matched_type< Args... > static_member_caller< Access, T >::declval_matched ( )
staticnoexcept

Like declval_matched(Args&&...) but is more convenient when you have argument types handy rather than argument expressions.

◆ declval_matched() [2/2]

template<class Access, class... T>
template<class... Args>
matched_type< Args... > static_member_caller< Access, T >::declval_matched ( Args && ...)
staticnoexcept

Can be used like std::declval, passing it the type whose named method the arguments would be forwarded to.

◆ fallback()

template<class Access, class... T>
template<class F>
constexpr member_::fallback_caller< static_member_caller, std::remove_reference_t< F > > static_member_caller< Access, T >::fallback ( F && functor)
inlinestaticconstexpr
Returns
A callable object which, when called, will forward its arguments to call if possible, or forward them to its functor if possible, or finally try calling the functor with no arguments.

◆ is_matched()

template<class Access, class... T>
template<class... Args>
constexpr bool static_member_caller< Access, T >::is_matched ( Args && ...)
inlinestaticconstexpr

true if the call with the provided arguments would be valid.

◆ operator()()

template<class Access, class... T>
template<class... Args>
decltype(auto) static_member_caller< Access, T >::operator() ( Args &&... args) const
inlineconstexpr

An instance of static_member_caller is callable using its operator (). It simply forwards its arguments, using std::forward, to its static call method.

◆ safe_declval_matched() [1/2]

template<class Access, class... T>
template<class... Args>
safe_matched_type< Args... > static_member_caller< Access, T >::safe_declval_matched ( )
staticnoexcept

◆ safe_declval_matched() [2/2]

template<class Access, class... T>
template<class... Args>
safe_matched_type< Args... > static_member_caller< Access, T >::safe_declval_matched ( Args && ...)
staticnoexcept

Like declval_matched(Args&&...) but it will create a void expression instead of a compile error. This is likely to cause a compile error in whatever you were trying to do anyway.

Member Data Documentation

◆ is_matched_v

template<class Access, class... T>
template<class... Args>
bool static_member_caller< Access, T >::is_matched_v
staticconstexpr
Initial value:
=
caller<void, types_list, Args...>::is_callable

true if one of the listed types has a static method named by the Access template parameter, which would accept the arguments of the same type as Args....


The documentation for this struct was generated from the following file: