From 8163b7d8c18e948090ed2e8a17e9665e906264de Mon Sep 17 00:00:00 2001 From: Sudrut Date: Thu, 18 Dec 2025 23:37:20 +0800 Subject: [PATCH 1/3] fix: move main_function to basic_concepts/ --- .../docs/cpp/language/{ => basic_concepts}/main_function.mdx | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/content/docs/cpp/language/{ => basic_concepts}/main_function.mdx (100%) diff --git a/src/content/docs/cpp/language/main_function.mdx b/src/content/docs/cpp/language/basic_concepts/main_function.mdx similarity index 100% rename from src/content/docs/cpp/language/main_function.mdx rename to src/content/docs/cpp/language/basic_concepts/main_function.mdx From c30f5e70be3467a3a026cb328a048b298a8262ff Mon Sep 17 00:00:00 2001 From: Sudrut Date: Thu, 18 Dec 2025 23:54:39 +0800 Subject: [PATCH 2/3] fix: update main_function.mdx --- .../language/basic_concepts/main_function.mdx | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/content/docs/cpp/language/basic_concepts/main_function.mdx b/src/content/docs/cpp/language/basic_concepts/main_function.mdx index f9642ed..5f86407 100644 --- a/src/content/docs/cpp/language/basic_concepts/main_function.mdx +++ b/src/content/docs/cpp/language/basic_concepts/main_function.mdx @@ -54,7 +54,7 @@ The C++ standard recommends implementation-defined main functions to place the e Non-negative value representing the number of arguments passed to the program from the environment in which the program is run. - Pointer to the first element of an array of `argc + 1` pointers, of which the last one is null and the previous ones, if any, point to null-terminated multibyte strings that represent the arguments passed to the program from the execution environment. If `argv[0]` is not a null pointer (or, equivalently, if `argc > 0`), it points to a string that represents the name used to invoke the program, or to an empty string. + Pointer to the first element of an array of `argc + 1` pointers, of which the last one is null and the previous ones, if any, point to null-terminated multibyte strings that represent the arguments passed to the program from the execution environment. If `argv[0]` is not a null pointer (or, equivalently, if `argc > 0`), it points to a string that represents the name used to invoke the program, or to an empty string. The body of the `main` function. @@ -63,30 +63,30 @@ The C++ standard recommends implementation-defined main functions to place the e ## Explanation -The `main` function is called at program startup after initialization of the non-local objects with static storage duration. It is the designated entry point to a program that is executed in hosted environment (that is, with an operating system). The entry points to freestanding programs (boot loaders, OS kernels, etc) are implementation-defined. +The `main` function is called at program startup after initialization of the non-local objects with static storage duration. It is the designated entry point to a program that is executed in hosted environment (that is, with an operating system). The entry points to freestanding programs (boot loaders, OS kernels, etc) are implementation-defined. -The parameters of the two-parameter form of the `main` function allow arbitrary multibyte character strings to be passed from the execution environment (these are typically known as _command line arguments_), the pointers [`argv[1]`, `argv[argc - 1]`] point at the first characters in each of these strings. `argv[0]` (if non-null) is the pointer to the initial character of a null-terminated multibyte string that represents the name used to invoke the program itself (or an empty string `""` if this is not supported by the execution environment). The strings are modifiable, although these modifications do not propagate back to the execution environment: they can be used, for example, with `std::strtok`. The size of the array pointed to by `argv` is at least `argc + 1`, and the last element, `argv[argc]`, is guaranteed to be a null pointer. +The parameters of the two-parameter form of the `main` function allow arbitrary multibyte character strings to be passed from the execution environment (these are typically known as _command line arguments_), the pointers [`argv[1]`, `argv[argc - 1]`] point at the first characters in each of these strings. `argv[0]` (if non-null) is the pointer to the initial character of a null-terminated multibyte string that represents the name used to invoke the program itself (or an empty string `""` if this is not supported by the execution environment). The strings are modifiable, although these modifications do not propagate back to the execution environment: they can be used, for example, with `std::strtok`. The size of the array pointed to by `argv` is at least `argc + 1`, and the last element, `argv[argc]`, is guaranteed to be a null pointer. The `main` function has the following several special properties: -- The body of the `main` function does not need to contain the `return` statement: if control reaches the end of main without encountering a `return` statement, the effect is that of executing `return 0;`. -- Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration and evaluates any postcondition assertions of main) and then calling `std::exit` with the same argument as the argument of the return (`std::exit` then destroys static objects and terminates the program). +- The body of the `main` function does not need to contain the `return` statement: if control reaches the end of main without encountering a `return` statement, the effect is that of executing `return 0;`. +- Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration and evaluates any postcondition assertions of main) and then calling `std::exit` with the same argument as the argument of the return (`std::exit` then destroys static objects and terminates the program). The `main` function has several restrictions (violation of which renders the program ill-formed): -- It cannot be named anywhere in the program +- It cannot be named anywhere in the program - in particular, it cannot be called recursively - its address cannot be taken - - it cannot be used in a `typeid` expression or a `decltype` specifier -- It cannot be predefined and cannot be overloaded: effectively, the name `main` in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that an entity named `main` cannot be declared with C language linkage in any namespace). -- It cannot be defined as deleted or declared with any language linkage, constexpr, consteval, inline, or static. + - it cannot be used in a `typeid` expression or a `decltype` specifier +- It cannot be predefined and cannot be overloaded: effectively, the name `main` in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that an entity named `main` cannot be declared with C language linkage in any namespace). +- It cannot be defined as deleted or declared with any language linkage, constexpr, consteval, inline, or static. - The return type of the `main` function cannot be deduced (`auto main() {...}` is not allowed). -- The `main` function cannot be a coroutine. -- The `main` function cannot attach to a named module. +- The `main` function cannot be a coroutine. +- The `main` function cannot attach to a named module. ## Notes -If the `main` function is defined with a function `try` block, the exceptions thrown by the destructors of static objects (which are destroyed by the implied `std::exit`) are not caught by it. +If the `main` function is defined with a function `try` block, the exceptions thrown by the destructors of static objects (which are destroyed by the implied `std::exit`) are not caught by it. The manner in which the arguments given at the OS command line are converted into the multibyte character arrays referenced by `argv` may involve implementation-defined processing: @@ -187,6 +187,6 @@ The following behavior-changing defect reports were applied retroactively to pre - C documentation for **main function** + C documentation for **main function** From 4fc3346ba8eeba6ad0373052bd354af501484e2b Mon Sep 17 00:00:00 2001 From: Sudrut Date: Thu, 18 Dec 2025 23:56:31 +0800 Subject: [PATCH 3/3] fix: remove `Missing` --- src/content/docs/cpp/language/basic_concepts/main_function.mdx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/content/docs/cpp/language/basic_concepts/main_function.mdx b/src/content/docs/cpp/language/basic_concepts/main_function.mdx index 5f86407..ec15835 100644 --- a/src/content/docs/cpp/language/basic_concepts/main_function.mdx +++ b/src/content/docs/cpp/language/basic_concepts/main_function.mdx @@ -8,7 +8,6 @@ import Behavior from "@components/Behavior.astro"; import { Decl, DeclDoc } from "@components/decl-doc"; import { DR, DRList } from "@components/defect-report"; import { Desc, DescList, DocLink } from '@components/index'; -import Missing from "@components/Missing.astro"; import { ParamDoc, ParamDocList } from "@components/param-doc"; import { Revision } from "@components/revision"; import WG21PaperLink from "@components/WG21PaperLink.astro";