First, some boilerplate to give us a SFINAE friendly invoke type test:
namespace invoke_details {
template<class Sig,class=void> struct invoke {};
template<class F, class...Args> struct invoke<
F(Args...),
void( decltype( std::declval<F>(Args...) ) )
> {
using type=decltype( std::declval<F>(Args...) );
};
}
template<class Sig> using invoke=typename invoke_details::invoke<Sig>::type;
now invoke< Foo(int, int) >
is the type you get when you take a variable of type Foo
and invoke it with two int
s, and it evaluates in a SFINAE friendly manner.
This is basically a SFINAE friendly std::result_of
.
Next, some more pretty stuff. result_type
and param_type
save on typing elsewhere:
template<class T>
using result_type = typename T::result_type;
template<class T>
using param_type = typename T::param_type;
details::has_property< X, T >
will take a template X
and apply T
. If this succeeds, it is true_type
, otherwise false_type
:
namespace details {
template<template<class>class X, class T, class=void>
struct has_property : std::false_type {};
template<template<class>class X, class T>
struct has_property<X,T,void(X<T>)> : std::true_type {};
}
This gives us has_result_type
etc in a pretty way:
template<class T>
using has_result_type = details::has_property< result_type, T >;
template<class T>
using has_param_type = details::has_property< param_type, T >;
template<class Sig>
using can_invoke = details::has_property< invoke, Sig >;
template<class T>
using can_twist_invoke = can_invoke< T(std::mt19937) >;
I think the simplicity of these declarations is worth the earlier boilerplate.
Now, a bit of boolean metaprogramming:
template<bool...> struct all_of : std::true_type {};
template<bool b0, bool... bs> struct all_of : std::integral_constant< bool, b0 && all_of<bs...>{} > {};
template<class T, template<class>class... Tests>
using passes_tests = all_of< Tests<T>{}... >;
And we get our one line pretty is_distribution
:
template<class T>
using is_distribution = passes_tests< T, has_result_type, has_param_type, can_twist_invoke >;
This does not yet cover .param
or .reset
.
This style leads to more code, but the "nasty" stuff gets hidden away in details namespaces. Someone who sees is_distribution
can look at the definition and see self-documented what it means. Only after drilling down will they see the messier implementation details.
mt19937
.