Who Likes Template Metaprogramming?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
chili
Site Admin
Posts: 3852
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Who Likes Template Metaprogramming?

Post by chili » May 21st, 2019, 3:01 pm

Now's your chance to prove how big ya dick:

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

albinopapa
Posts: 3825
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Who Likes Template Metaprogramming?

Post by albinopapa » May 21st, 2019, 3:55 pm

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
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 3825
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Who Likes Template Metaprogramming?

Post by albinopapa » May 21st, 2019, 4:08 pm

using call_type = function_traits<decltype(&F::type::operator())>;

If F is a lambda, where does the ::type come into play?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3852
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Who Likes Template Metaprogramming?

Post by chili » May 21st, 2019, 4:20 pm

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

Post Reply