Who Likes Template Metaprogramming?
Posted: May 21st, 2019, 3:01 pm
Learn to program or die trying
https://forum.planetchili.net:443/
Code: Select all
template<class F, class...Args>
void Register( F&& f, Args&&... args )
{
static_assert(std::is_invocable<F, args...>::value,"Handler must be invocable.");
// ...
}
Code: Select all
template<typename T>
struct is_callable_impl
{
private:
typedef char( &yes )[1];
typedef char( &no )[2];
struct Fallback
{
void operator()();
};
struct Derived : T,Fallback
{};
template<typename U,U> struct Check;
template<typename>
static yes test( ... );
template<typename C>
static no test( Check<void (Fallback::*)(),&C::operator()>* );
public:
static const bool value = sizeof( test<Derived>( 0 ) ) == sizeof( yes );
};
template<typename T>
struct is_callable
: std::conditional<
std::is_class<T>::value,
is_callable_impl<T>,
std::false_type
>::type
{};
Code: Select all
template <typename T>
struct function_traits
: public function_traits<decltype(&T::operator())>
{};
// For generic types, directly use the result of the signature of its 'operator()'
template <typename ClassType,typename ReturnType,typename... Args>
struct function_traits<ReturnType( ClassType::* )(Args...) const>
// we specialize for pointers to member function
{
enum
{
arity = sizeof...(Args)
};
// arity is the number of arguments.
typedef ReturnType result_type;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i,std::tuple<Args...>>::type type;
// the i-th argument is equivalent to the i-th tuple element of a tuple
// composed of those arguments.
};
};