使用类型的参数包和相应值的输入数组构造类型
Constructing a type using parameter pack of types and an input array of corresponding values
在发布这个问题之前,我已经尝试适应 here, and here 中提到的方法。但是,没有什么可以解决我的要求,因为我有一个需要一些调整的中间函数。
基本上,我有如下。我必须根据类型存储在参数包 Args
中的参数和数组 ARGValue *ptr
中的相应值构造类型 T
但是,要将 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) ;
}
};
我该如何解决这个问题?如何安全地从 ARGValue*
获取 Args...
中所有类型的值,然后构造类型 T
?
我不想为多个参数创建特化,因为它可能会使代码混乱。
看来你想要:
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])))...) ;
}
};
在发布这个问题之前,我已经尝试适应 here,
基本上,我有如下。我必须根据类型存储在参数包 Args
中的参数和数组 ARGValue *ptr
T
但是,要将 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) ;
}
};
我该如何解决这个问题?如何安全地从 ARGValue*
获取 Args...
中所有类型的值,然后构造类型 T
?
我不想为多个参数创建特化,因为它可能会使代码混乱。
看来你想要:
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])))...) ;
}
};