lundi 13 avril 2015

I got error by lazy invoking of fabric method with variadic templates . why?

I have found code below from this site, but I can't to adapting for my purpose:


I have factory method for creating objects with variadics, and have function `apply' for invoke the method from factory and unpack to there args saved before into std::tuple...


thanks for any help !



#include <iostream>
#include <utility>
#include <iostream>
#include <functional>
#include <utility>
#include <tuple>

class A {
public:
A(int, int){}
};

class B : public A {
public:
B(int x, int y):A(x, y){}
};





template <class Base, class Derived>
class BaseFactory {
public:

template <class ...Args>
static Base* create(Args&&... args){
return new Derived(std::forward<Args>(args)...);
}

};




// ------------- UTILITY---------------
template<int...> struct index_tuple{};

template<int I, typename IndexTuple, typename... Types>
struct make_indexes_impl;

template<int I, int... Indexes, typename T, typename ... Types>
struct make_indexes_impl<I, index_tuple<Indexes...>, T, Types...> {
typedef typename make_indexes_impl<I + 1, index_tuple<Indexes..., I>, Types...>::type type;
};

template<int I, int... Indexes>
struct make_indexes_impl<I, index_tuple<Indexes...> > {
typedef index_tuple<Indexes...> type;
};

template<typename ... Types>
struct make_indexes : make_indexes_impl<0, index_tuple<>, Types...> {};



// ----------UNPACK TUPLE AND APPLY TO FUNCTION ---------

template<class Ret, class... Args, int... Indexes >
Ret apply_helper( Ret (*pf)(Args...), index_tuple< Indexes... >, std::tuple<Args...>&& tup){
return pf( std::forward<Args>( std::get<Indexes>(tup))... );
}

template<class Ret, class ... Args>
Ret apply(Ret (*pf)(Args...), const std::tuple<Args...>& tup){
return apply_helper(pf, typename make_indexes<Args...>::type(), std::tuple<Args...>(tup));
}

template<class Ret, class ... Args>
Ret apply(Ret (*pf)(Args...), std::tuple<Args...>&& tup){
return apply_helper(pf, typename make_indexes<Args...>::type(), std::forward<std::tuple<Args...>>(tup));
}




int main(){

// I got error: apply(<unresolved overloaded function type>, std::tuple<int, int>)
A* = apply(&BaseFactory<A, B>::create<int,int>, std::make_tuple(5, 25));

return 0;
}

Aucun commentaire:

Enregistrer un commentaire