将argv和safe转换为元组
Convert argv and safe into tuple
是否有可能在整数中转换任意长度的字符数组并将其安全转换为元组?
下面是一个例子:
int main(int argc, const char* argv[]) {
auto input = convert(argc, argv);
}
所以如果我调用main:
<我>。/main 1 我>
input = std::tuple<int, int>(1, 2);
:
<我>。/主要2 我>
input = std::tuple<int>(2);
我希望你明白我在找什么。
一个动态大小的元组,只有一个类型?你要找的是std::vector<int>
。
元组中的元素个数在编译时固定。
我对你收到的这个问题的答案不满意,所以我为你制定了一个解决方案。这并不意味着我同意你的想法是好的:-)
将动态数据转换为静态多态功能时的问题是,您必须准备好预先构建的专用性数组-在运行时环境中每种可能性都有一个。双重分派习惯用法也存在同样的问题。
下面是一个程序的最小演示,该程序将argv
转换为适当的tuple
,然后调用模板函数(在元组的模板参数上模板化)。
我将参数的数量限制为5 +程序名,但是您可以通过在数组switch_functions
中添加条目来增加这个限制。
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
// this is a template of a function that does something with a tuple of strings of any size.
// it's left to the reader to figure out what to do with it.
template<class...Args>
void do_something_with_tuple(const std::tuple<Args...>& tuple)
{
std::cout << "hello! " << sizeof...(Args) << std::endl;
}
// this is a type that manufactures a tuple of <size> strings
template<size_t size>
struct make_string_tuple
{
using rest = typename make_string_tuple<size-1>::type;
using mine = std::tuple<std::string>;
using type = decltype(std::tuple_cat(std::declval<mine>(), std::declval<rest>()));
};
// this is the terminator specialisation that ends the sequence of strings in a tuple
template<>
struct make_string_tuple<0> {
using type = std::tuple<>;
};
// this is a convenience typedef that obviates the use of the typename keyword
template<size_t size>
using make_string_tuple_t = typename make_string_tuple<size>::type;
// this is a function that initialises one element of a tuple from a corresponding position in a vector
template<size_t i, size_t n>
struct init_tuple{
static void apply(make_string_tuple_t<n>& t, const std::vector<std::string>& x)
{
std::get<i>(t) = x[i];
init_tuple<i+1, n>::apply(t, x);
}
};
// this is the terminator specialisation for the above
template<size_t n>
struct init_tuple<n, n> {
static void apply(make_string_tuple_t<n>& t, const std::vector<std::string>&)
{
}
};
// a template function that manufactures a tuple of size <size>, populates it from a vector
// which must be at least as large, and then calls the template function do_something_with_tuple
template<size_t size>
void call_it(const std::vector<std::string>& x)
{
using tuple_type = make_string_tuple_t<size>;
tuple_type t;
init_tuple<0, size>::apply(t, x);
do_something_with_tuple(t);
}
// a test program
auto main(int argc, char**argv) -> int
{
// the type of function to call with a vector of arguments
using switch_function = void (*)(const std::vector<std::string>&);
// a vector of pointers to concrete versions of call_it
static const switch_function switch_functions[] = {
call_it<0>,
call_it<1>,
call_it<2>,
call_it<3>,
call_it<4>,
call_it<5>,
call_it<6>,
};
// convert argv into a vector of strings
std::vector<std::string> x;
while (argc--) {
x.emplace_back(*argv++);
}
// call the appropriate version of call_it
switch_functions[x.size()](x);
return 0;
}
相关文章:
- 将元组的向量转换/构造为堆
- 如何在编译时将齐次元组转换为数组?
- Boost.Hana:在 constexpr 上下文中将值元组转换为相应类型的元组
- 将多个数组转换为元组
- 将函数应用于元组中的每个元素,将每个元素强制转换为类型包中的不同类型,然后作为参数包传递
- 为什么此元组到引用元组 (std::tie) 转换有效?
- 将树转换为元组的编译时转换
- 如何将标准::数组转换为标准::元组?
- 如何将元组转换为C 11中的字节数组
- 元组尺寸默认构造函数及其转换为std :: size_t
- 使用 hana::transform 在 C++14 中转换元组内的类型
- 将STD ::变体转换为STD ::模板类实例的元组
- 将 integral_constants 元组转换为整数元组
- 错误:无法从"元组<[...]、std::__1::元组<无符号长长、无符号长长>>"到"常量元组<[...],uint_type>"的可行转换
- 将元组转换为变体
- Boost::P ython,将元组转换为Python有效,矢量<tuple>不
- 有没有一种方法可以将左值和右值的列表分别转换为具有引用类型和完整类型的元组
- 将元组转换为函数参数
- 如何使用笛卡尔积将向量元组转换为元组的向量
- 将std::元组转换为模板参数包