我有一个函数main_func
,在其中我通过将参数包/变量模板转换为元组来对其进行修改。修改后,例如:original = tuple<int, float, string>
变为modified = tuple<int float>
我想将修改后的元组扩展为示例get_type_vector
中的另一个函数,以便参数包表示修改后的元组Args = int, float
的类型。
template<typename... Args> void main_func() { // Modify the parameter pack (which if im correct can only be done by converting to tuple?) using original = std::tuple<Args...>; using modified = // something that alters the types of the original tuple // This is what i want to accomplish // somehow expand the tuple type like a parameter pack auto vec = get_type_vector<modified...>() } // Returns a vector of type_info template<typename... Args> std::vector<type_info> get_type_vector() { return { type_info<Args>()... }; }
是否可以以某种方式扩展元组类型,例如参数包的类型?我发现了使用
std::apply
等的示例,但这需要您具有一个值,而不仅仅是一个元组的typedef。
我有一个函数main_func,通过将其转换为元组来修改参数包/变量模板。修改完之后,例如:original = tuple
您可以通过引入间接层轻松地扩展元组,间接层可以是lambda(C ++ 20)或模板函数(C ++ 11)。例如:
一种相当简单的方法就是重载,让编译器推断类型。 std::type_identity
在这里很有用(C ++ 20,但可以在任何版本的C ++中轻松复制)。它可以根据类型创建简单的廉价标签
真的没有必要,因为元组已经有一个typedef /用于此。