将argv和safe转换为元组

Convert argv and safe into tuple

本文关键字:元组 转换 safe argv      更新时间:2023-10-16

是否有可能在整数中转换任意长度的字符数组并将其安全转换为元组?

下面是一个例子:

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;
}