samedi 3 juin 2017

Deduce dependent type in a template specialization

I'm trying to use function_traits class to deduce the lambda's operator() member function pointer with common known syntax (Class::*pointer)(Args list). But the problem is in typedefs/using in the specialization. It looks like compiler is not using my traits' class template specialization. And stucks to the general case Compiler says

No type named 'result_type' in 'function_traits'

No type named 'FunctionsType' in 'function_traits'

#include <functional>
#include <iostream>

using namespace std;

template <typename T>
struct function_traits {};

template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) >
// we specialize for pointers to member function
{
    using result_type = ReturnType;
    using FunctionsType = ReturnType(ClassType::*)(Args...);

    using arg_tuple = std::tuple<Args...>;
    static constexpr auto arity = sizeof...(Args);
};

template <typename F>
struct lambda_expression_my
{
    typedef function_traits<decltype(&F::operator())> traits;
    typedef typename traits::result_type RT;
    typedef typename traits::FunctionsType FunctionType;

    F _object;
    FunctionType _function;

    lambda_expression_my(const F & object) :
    _object(object),
    _function(&F::operator()) {}

    template <typename ... Args>
    RT operator() (Args ... args) const {
        return (_object.*_function)(args...);
    }
};

Aucun commentaire:

Enregistrer un commentaire