Skip to content

[PROPOSE] Refactor args_t to use ranges instead of vector #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <ranges>

#ifndef CPP2_NO_EXCEPTIONS
#include <exception>
Expand Down Expand Up @@ -1493,25 +1494,26 @@ inline auto to_string(std::tuple<Ts...> const& t) -> std::string

//-----------------------------------------------------------------------
//
// args: see main() arguments as vector<string_view>
// args: see main() arguments as std::ranges::view<string_view>
//
//-----------------------------------------------------------------------
//
struct args_t : std::vector<std::string_view>
struct args_t : std::ranges::transform_view<
std::span<char const* const>,
decltype([](char const* s) { return std::string_view{s}; })
>
{
args_t(int c, char const* const* v) : vector{static_cast<size_t>(c)}, argc{c}, argv{v} {}

int argc = 0;
char const* const* argv = nullptr;
};

inline auto make_args(int argc, char const* const* argv) -> args_t
{
auto ret = args_t{argc, argv};
for (auto i = 0; i < argc; ++i) {
ret[i] = std::string_view{argv[i]};
}
return ret;
return args_t{
{ std::span(argv, argc), {} },
argc,
argv
};
}


Expand Down