C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Floating an idea: int main(std::span<std::string_view> args)

From: Tom Honermann <tom_at_[hidden]>
Date: Sat, 7 Sep 2024 16:41:32 -0400
Rather than extending the signature of main(), I would prefer to focus
on an approach that makes program arguments accessible from outside of
main. There is a WG14 proposal for C that does so:

  * N2948 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2948.pdf>:
    Accessing the command line arguments outside of main()

Such an approach enables global initializers and other code that runs
before main() to access the command line (e.g., for diagnostic or
logging purposes). The command line could be exposed in a non-mutable
form by such an interface.

Tom.

On 9/7/24 11:37 AM, Jeremy Rifkin via Std-Proposals wrote:
> Hello,
>
> I'd like to propose adding int main(std::span<std::string_view> args) as
> a standard signature for main.
>
> Motivation:
> - Modernization
> - Ergonomics
> - Teachability
> - Modern C++ through and through instead of 'what's a char**' and
> 'argv[1] == "--help" is a footgun, beware'
>
> Considerations:
> - There would be slight startup overhead in constructing the
> std::string_views and underlying array of string views, however, this
> would be entirely opt-in
> - It would probably add some implementation complexity
> - This would be an aspect of the core language which depends on the
> standard library. I don't love that, but, there is precedence for this
> sort of thing
> - Direct mutability of the argument contents would be lost
>
> I would make a reference implementation as part of the proposal.
>
>
> Cheers,
> Jeremy

Received on 2024-09-07 20:41:36