C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sat, 07 Sep 2024 19:41:50 +0200
On Saturday 7 September 2024 17:37:22 CEST 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'

This has been discussed before on the mailing list.

> 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

And it consumes stack space. in the extreme case of 65536 1-byte arguments
(including the executable name), that's an additional 1 MB of stack space
required, in addition to the 512 kB already consumed by the existing argv
array. For a system with 2 MB stack space (which isn't hard to imagine), that
reduces the space available to the application by two thirds.

Another alternative that has been proposed is that it's a std::argument_list
type that, through compiler magic, happens to not consume any stack space,
retains the current modifiable behaviour, and retains the O(n) length
calculations for the arguments.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2024-09-07 17:41:56