使用类型的参数包和相应值的输入数组构造类型
Constructing a type using parameter pack of types and an input array of corresponding values
在发布这个问题之前,我已经尝试适应这里,这里和这里提到的方法。但是,没有什么可以解决我的需求,因为我有一个需要一些调整的中间函数。
基本上,我拥有的如下。我必须根据类型存储在参数包Args
中的参数和数组中的相应值来构造类型T
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) ;
}
};
我该如何解决这个问题?如何安全地从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])))...) ;
}
};
相关文章:
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 当我输入字符类型的数字时,为什么我无法获得整数?
- C++模板编程设计问题 - 根据输入文件返回不同的类型
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 需要有关运行具有输入类型的编译器的帮助
- 如何在 CPP 中检查给定输入的数据类型?
- 将空格作为单字符数据类型的输入?
- 在函数中使用运算符重载,在 c++ 中使用 const 类型输入参数
- 有没有办法将输入类型包装到C++中的模板类?
- 如果输入类型与目标类型不同,"cin"变量是否重置为某个默认值?
- 函数重载并根据相同的输入返回不同的数据类型
- C++ 从用户那里获取未知数据类型输入
- C 异质容器,作为类型输入
- 如何在无效的数据类型输入后继续循环
- WSDLPull 使用复杂类型输入
- 使用std::cin (c++)检查有效的类型输入
- 为已定义的变量传递错误的类型输入
- 如何验证数据类型输入
- c++中的混合数据类型输入