C++ Logo

std-proposals

Advanced search

Re: [std-proposals] modern method to access command line arguments

From: connor horman <chorman64_at_[hidden]>
Date: Tue, 16 Sep 2025 23:18:20 -0400
Not going to voice support or opposition in this reply.
BTW, there's an easy way to implement this: Compile this signature of main
with name mangling, and synthesize unmangled main for OS entry point.

On Tue, Sep 16, 2025 at 23:00 Emily Ammundsen via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hello,
>
> My current understanding is that the only portable way to access
> command-line arguments in C++ is with `int main(int argc, char** argv)`.
> Given that modern C++ avoids pointer arithmetic and indexing into raw
> pointers, it would be nice if there was an alternative way to access
> command-line arguments.
>
> For example, there could be an additional main definition of `int
> main(std::command_line_arguments args)` where
> `std::command_line_arguments` is type which implements the
> `std::ranges::random_access_range` concept. Dereferencing the iterator
> would result in instances of `std::string` (or perhaps `std::string_view`
> or `std::zstring_view` from P3655R2). This would allow a range-for loop
> over your command line arguments and the potential for out-of-bounds
> checking.
>
> An alternative method would be to introduce new function named something
> like `std::get_command_line_arguments()` which would return an instance of
> `std::command_line_arguments` and encourage people to use `int main()` as
> their main signature.
>
> If we wished to include standardized support for `char** envp`, that is a
> third argument passed to `main` which holds a pointer to the environment
> variables, which is a common C++ compiler extension, it could go into a
> field of `std::command_line_arguments` though at that point renaming
> `std::command_line_arguments` to something else like
> `std::program_start_context` would be appropriate, and would allow other
> additions in later versions of C++.
>
> The type signature of `char** argv` suggests that modifying the strings
> inside `argv` is permitted, and this suggests a `std::string`
> representation. However, my impression is that this is not recommended
> practice.
>
> There is hypothetical potential for a slight performance improvement if an
> operating system stores command line arguments in a non-null terminated
> string format and thus can pass a `std::string_view` to main without
> needing to do a copy. I am not familiar with this part of operating
> systems, but I have doubts that this performance gain would be realized in
> practice.
>
> If the operating system already supplies null-terminated command line
> arguments like they must currently, then using `std::string_view` instead
> of `std::string` or `std::zstring_view` could imply a small performance
> decrease when interacting with legacy APIs that need null-terminated
> strings.
>
> I tried to look for previous work on this matter and I failed to find it.
> If I merely didn't search hard enough, then I apologize.
>
> Emily
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-09-17 03:18:35