Page 1 of 1

Who Likes Template Metaprogramming?

Posted: May 21st, 2019, 3:01 pm
by chili
Now's your chance to prove how big ya dick:

https://stackoverflow.com/questions/562 ... -detection

Re: Who Likes Template Metaprogramming?

Posted: May 21st, 2019, 3:55 pm
by albinopapa
Taking a stab in the dark here, too wrapped up and tired to test, but cppreference.com/is_invocable

Shouldn't you also have the parameter pack along with <F>?
The documentation seems to say that it checks if the function is callable with the parameters supplied to the parameter pack args..., so

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.");
        // ...
   }
example

Re: Who Likes Template Metaprogramming?

Posted: May 21st, 2019, 4:08 pm
by albinopapa
using call_type = function_traits<decltype(&F::type::operator())>;

If F is a lambda, where does the ::type come into play?

Re: Who Likes Template Metaprogramming?

Posted: May 21st, 2019, 4:20 pm
by chili
Yeah, I found something nice to fix that secondary problem:

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
{};
And this solved my lambda problem with function_traits

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.
	};
};