X
Hallo Martin,
ich weiß zwar nicht, was du vor hast, aber möchtest du einfach eine Factory,
der du einmal die Parameter übergibst und dann nur noch create aufrufst?
Wahrscheinlich lieg ich völlig falsch, aber vielleicht willst du sowas:
#include <utility>
#include <tuple>
#include <boost/type_traits/remove_const.hpp>
struct Foo{
Foo(int i, char c){}
};
template<unsigned int I, typename Type, typename... TParams>
struct factory_helper{
template<typename TTuple, typename... TParams2>
static Type create_instance(TTuple& tuple, TParams2... params){
typedef typename std::tuple_element<I-1, typename boost::remove_const<TTuple>::type >::type forward_type;
return factory_helper<I-1, Type, TParams...>::template create_instance<TTuple, forward_type, TParams2...>(tuple, std::get<I-1>(tuple), params...);
}
};
template<class Type, typename... TParams>
struct factory_helper<0, Type, TParams...>{
typedef std::tuple<TParams...> tuple_type;
template<typename TTuple, typename... TParams2>
static Type create_instance(TTuple& tuple, TParams2... params){
return Type(params...);
}
};
template<class Type, typename... TParams>
class factory{
private:
public:
factory(TParams... params) : _params(params...) {}
Type operator()() const{
return factory_helper<sizeof...(TParams), Type, TParams...>::create_instance(_params);
}
private:
std::tuple<TParams...> _params;
};
template<typename Type, typename... TParams>
static factory<Type, TParams...> create_factory(TParams... params){
return factory<Type, TParams...>(params...);
}
int main()
{
auto factory = create_factory<Foo>(1,'c');
Foo foo = factory();
}
Gruß,
XSpille