在发布此问题之前,我已尝试使用here,here和here上提到的方法。但是,没有什么能解决我的要求,因为我有一个需要进行一些调整的中间功能。
基本上,我所拥有的内容如下。我必须基于其类型存储在参数包T
中的参数和数组Args
中的相应值1>来构造类型ARGValue *ptr
但是,要将ARGValue* ptr
中的类型值映射到Args...
中的类型
我需要做一个中间操作,如下所示。
// Specialization for 1 argument
template<typename T,typename... Args>
struct instance_helper<T,1,Args...>
{
static T invokeConstructor(const ARGValue *ptr)
{
// Intermediate operation
typedef typename std::tuple_element<0, std::tuple<Args...> >::type TT;
const auto val0 = *(someUglyCast<TT*>(ptr[0])) ;
// Return the desired type
return T(val0);
}
};
//Specialization for no argument
template<typename T,typename... Args>
struct instance_helper<T,0,Args...>
{
static T invokeConstructor(const ARGValue*)
{
return T();
}
};
// General Case
template<typename T,size_t packSize,typename... Args>
struct instance_helper
{
static T invokeConstructor(const ARGValue *ptr)
{
// Do some recursive magic here for intermediate operation
return T(//Do some recursive magic) ;
}
};
我该如何解决这个问题?如何从Args...
安全地获取ARGValue*
中所有类型的值,然后构建类型T
?
我不想为多个参数创建特化,因为它可能会使代码混乱。
答案 0 :(得分:3)
似乎你想要:
template<typename T,size_t packSize,typename... Args>
struct instance_helper
{
static T invokeConstructor(const ARGValue *ptr)
{
return invokeConstructor(std::make_index_sequence<packSize>{}, ptr);
}
private:
template <std::size_t ... Is>
static T invokeConstructor(std::index_sequence<Is...>, const ARGValue *ptr)
{
using Tuple = std::tuple<Args...>;
// Do some recursive magic here for intermediate operation
return T((*(someUglyCast<const std::tuple_element_t<Is, Tuple>*>(ptr[Is])))...) ;
}
};