FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Improves main function by mordante · Pull Request #63 · hsutter/cppfront · GitHub

Repository navigation

Improves main function - #63

Closed
mordante wants to merge 2 commits into
hsutter:mainfrom
mordante:improves_main_function
Closed

mordante wants to merge 2 commits into
hsutter:mainfrom
mordante:improves_main_function

Conversation

mordante commented Oct 9, 2022

Copy link
Copy Markdown

Two patches:

  1. Removes the forward declaration of main and its [[nodiscard]].
  2. Allows main to return void and allows a std::span<std::string_view> argument instead of int, char**.

This avoids some oddities with main:
- It will no longer be forward declared.
- It will no longer have the nodiscard attribute.
- It requires no arguments and a return type of int. Before when using
  arguments for this function they got wrapped with cpp2::in.

Since cpp2 doesn't support 'char**' it's not possible to use a main like
'int main(int argc, char** argv)`. This will be addressed in the next
commit.
This makes two improvements to main.

1. be honest regarding the return type

In C++ main has a special rule allowing it to not return the promised
value. Instead of allowing this artefact in cpp2, require that main
returns the promised value. When main returns no value, let the user
specify that by returning void.

2. make the arguments cpp2 compatible

The argv of main is not compatible with cpp2. Instead of making this a
special case in cpp2, use different parameter for main.
A std::span<std::string_view> is a better safe alternative for argv so
use that instead.
Note the implementation stores the argv in a std::vector, but it seems
better to use a std::span in the interface. If, in the future, a more
suitable container is added to C++ this doesn't change the interface in
the new main function.
It would be possible to use different parameters types like:
- std::vector<std::string_view>
- std::vector<std::string>
- std::span<std::string>
or even allow all 4 of them. I decided against multiple types to keep
things simple to teach and not let the number of options grow when more
suitable containers are added to the Standard, for example
   P0843 static_vector

Both improvements are implemented by adding a new implementation defined
function. This function cpp2__main replaces the name main and keeps the
body if the function. Then it generates a helper main function to call
cpp__main with the appropriate arguments.

This removes teaching the special exception for main in [stmt.return]/4
  Otherwise, flowing off the end of a function that is neither main
  ([basic.start.main]) nor a coroutine ([dcl.fct.def.coroutine]) results in
  undefined behavior.
hsutter self-assigned this Oct 9, 2022

hsutter commented Oct 9, 2022

Copy link
Copy Markdown
Owner

Thanks for the suggestion. I considered something like this (and am still considering it) but there are subtle issues... for example, using a span<string_view> breaks the use of the command-line arguments as null-terminated strings. See also this comment on issue 45 for discussion on safety of string literals in Cpp2... command-line arguments are a lot like string literals in this regard.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL