| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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.
|
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. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Two patches: