将元组 TYPE 扩展为可变参数模板?
Expand a tuple TYPE into a variadic template?
我有一个函数 main_func
,我通过将参数 pack/variadic 模板转换为元组来修改它。我修改后,ex: 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。
您可以通过引入一个间接层轻松扩展元组,该间接层可以是 lambda (C++20) 或模板函数 (C++11)。例如
std::tuple<int, float, char> t;
[]<typename... Ts>(std::tuple<Ts...>)
{
// use `Ts...` here
}(t);
你的情况:
template <typename T>
struct type_wrapper { using type = T; };
template<typename... Args>
std::vector<type_info> get_type_vector(type_wrapper<std::tuple<Args...>>)
{
return { type_info<Args>()... };
}
get_type_vector(type_wrapper<std::tuple<int, float, char>>{});
type_wrapper
class 防止在 运行 时无用的元组实例化。您可以在 C++20 中使用 std::type_identity
。
一个相当简单的方法就是重载并让编译器推断类型。 std::type_identity
在这里很有用(C++20,但很容易在任何版本的 C++ 中复制)。它可以根据类型创建简单的廉价标签
template<typename... Args>
std::vector<type_info> get_type_vector(std::type_identity<std::tuple<Args...>>)
{
return { type_info<Args>()... };
}
用就是写
auto vec = get_type_vector(std::type_identity<modified>{})
我有一个函数 main_func
,我通过将参数 pack/variadic 模板转换为元组来修改它。我修改后,ex: 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。
您可以通过引入一个间接层轻松扩展元组,该间接层可以是 lambda (C++20) 或模板函数 (C++11)。例如
std::tuple<int, float, char> t;
[]<typename... Ts>(std::tuple<Ts...>)
{
// use `Ts...` here
}(t);
你的情况:
template <typename T>
struct type_wrapper { using type = T; };
template<typename... Args>
std::vector<type_info> get_type_vector(type_wrapper<std::tuple<Args...>>)
{
return { type_info<Args>()... };
}
get_type_vector(type_wrapper<std::tuple<int, float, char>>{});
type_wrapper
class 防止在 运行 时无用的元组实例化。您可以在 C++20 中使用 std::type_identity
。
一个相当简单的方法就是重载并让编译器推断类型。 std::type_identity
在这里很有用(C++20,但很容易在任何版本的 C++ 中复制)。它可以根据类型创建简单的廉价标签
template<typename... Args>
std::vector<type_info> get_type_vector(std::type_identity<std::tuple<Args...>>)
{
return { type_info<Args>()... };
}
用就是写
auto vec = get_type_vector(std::type_identity<modified>{})