Skip to content

Commit a20eed6

Browse files
authored
fix: adjust main_function (#86)
* fix: move main_function to basic_concepts/ * fix: update main_function.mdx * fix: remove `Missing`
1 parent 3dcd834 commit a20eed6

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/content/docs/cpp/language/main_function.mdx renamed to src/content/docs/cpp/language/basic_concepts/main_function.mdx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Behavior from "@components/Behavior.astro";
88
import { Decl, DeclDoc } from "@components/decl-doc";
99
import { DR, DRList } from "@components/defect-report";
1010
import { Desc, DescList, DocLink } from '@components/index';
11-
import Missing from "@components/Missing.astro";
1211
import { ParamDoc, ParamDocList } from "@components/param-doc";
1312
import { Revision } from "@components/revision";
1413
import WG21PaperLink from "@components/WG21PaperLink.astro";
@@ -54,7 +53,7 @@ The C++ standard recommends implementation-defined main functions to place the e
5453
Non-negative value representing the number of arguments passed to the program from the environment in which the program is run.
5554
</ParamDoc>
5655
<ParamDoc name="argv">
57-
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 <Missing>null-terminated multibyte strings</Missing> 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.
56+
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 <DocLink dest="/cpp/library/text/multibyte">null-terminated multibyte strings</DocLink> 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.
5857
</ParamDoc>
5958
<ParamDoc name="body">
6059
The body of the `main` function.
@@ -63,30 +62,30 @@ The C++ standard recommends implementation-defined main functions to place the e
6362

6463
## Explanation
6564

66-
The `main` function is called at program startup after <Missing>initialization</Missing> of the non-local objects with static storage duration. It is the designated entry point to a program that is executed in <Missing>hosted</Missing> environment (that is, with an operating system). The entry points to <Missing>freestanding</Missing> programs (boot loaders, OS kernels, etc) are implementation-defined.
65+
The `main` function is called at program startup after <DocLink dest="/cpp/language/initialization">initialization</DocLink> of the non-local objects with static <DocLink dest="/cpp/language/declarations/storage_duration">storage duration</DocLink>. It is the designated entry point to a program that is executed in <DocLink dest="/cpp/library/freestanding">hosted</DocLink> environment (that is, with an operating system). The entry points to <DocLink dest="/cpp/library/freestanding">freestanding</DocLink> programs (boot loaders, OS kernels, etc) are <Behavior type="impl-def">implementation-defined</Behavior>.
6766

68-
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 <Missing>`std::strtok`</Missing>. 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.
67+
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 <DocLink dest="/cpp/library/text/byte/strtok">`std::strtok`</DocLink>. 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.
6968

7069
The `main` function has the following several special properties:
7170

72-
- The body of the `main` function does not need to contain the <Missing>`return` statement</Missing>: if control reaches the end of main without encountering a `return` statement, the effect is that of executing `return 0;`.
73-
- 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 <Revision since="C++26">and evaluates any <Missing>postcondition assertions</Missing> of main</Revision>) and then calling <Missing>`std::exit`</Missing> with the same argument as the argument of the return (<Missing>`std::exit`</Missing> then destroys static objects and terminates the program).
71+
- The body of the `main` function does not need to contain the <DocLink dest="/cpp/language/statements/return">`return` statement</DocLink>: if control reaches the end of main without encountering a `return` statement, the effect is that of executing `return 0;`.
72+
- 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<Revision since="C++26"> and evaluates any <DocLink dest="/cpp/language/functions/function" section="postcondition-assertions">postcondition assertions</DocLink> of main</Revision>) and then calling <DocLink dest="/cpp/library/utility/program/exit">`std::exit`</DocLink> with the same argument as the argument of the <DocLink dest="/cpp/language/statements/return">return</DocLink> (<DocLink dest="/cpp/library/utility/program/exit">`std::exit`</DocLink> then destroys static objects and terminates the program).
7473

7574
The `main` function has several restrictions (violation of which renders the program <Behavior kind="ill-formed">ill-formed</Behavior>):
7675

77-
- It cannot be <Missing>named</Missing> anywhere in the program
76+
- It cannot be <DocLink dest="/cpp/language/basic_concepts/definition" section="naming-a-function">named</DocLink> anywhere in the program
7877
- in particular, it cannot be called recursively
7978
- its address cannot be taken
80-
- it cannot be used in a <Missing>`typeid`</Missing> expression <Revision since="C++11">or a <Missing>`decltype`</Missing> specifier</Revision>
81-
- 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 <Missing>language linkage</Missing> in any namespace).
82-
- It cannot be <Revision since="C++11">defined as deleted or</Revision> declared with any language linkage<Revision since="C++11">, <Missing>constexpr</Missing></Revision><Revision since="C++20">, <Missing>consteval</Missing></Revision>, <Missing>inline</Missing>, or <Missing>static</Missing>.
79+
- it cannot be used in a <DocLink dest="/cpp/language/expressions/typeid">`typeid`</DocLink> expression<Revision since="C++11"> or a <DocLink dest="/cpp/language/declarations/decltype">`decltype`</DocLink> specifier</Revision>
80+
- 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 <DocLink dest="/cpp/language/declarations/language_linkage">language linkage</DocLink> in any namespace).
81+
- It cannot be<Revision since="C++11"> defined as deleted or</Revision> declared with any language linkage<Revision since="C++11">, <DocLink dest="/cpp/language/declarations/constexpr">constexpr</DocLink></Revision><Revision since="C++20">, <DocLink dest="/cpp/language/declarations/consteval">consteval</DocLink></Revision>, <DocLink dest="/cpp/language/declarations/inline">inline</DocLink>, or <DocLink dest="/cpp/language/classes/static">static</DocLink>.
8382
- <Revision since="C++14">The return type of the `main` function cannot be deduced (`auto main() {...}` is not allowed).</Revision>
84-
- <Revision since="C++20">The `main` function cannot be a <Missing>coroutine</Missing>.</Revision>
85-
- <Revision since="C++20">The `main` function cannot attach to a named <Missing>module</Missing>.</Revision>
83+
- <Revision since="C++20">The `main` function cannot be a <DocLink dest="/cpp/language/functions/coroutines">coroutine</DocLink>.</Revision>
84+
- <Revision since="C++20">The `main` function cannot attach to a named <DocLink dest="/cpp/language/basic_concepts/modules">module</DocLink>.</Revision>
8685

8786
## Notes
8887

89-
If the `main` function is defined with a <Missing>function `try` block</Missing>, the exceptions thrown by the destructors of static objects (which are destroyed by the implied <Missing>`std::exit`</Missing>) are not <Missing>caught</Missing> by it.
88+
If the `main` function is defined with a <DocLink dest="/cpp/language/exceptions/try">function `try` block</DocLink>, the exceptions thrown by the destructors of static objects (which are destroyed by the implied <DocLink dest="/cpp/library/utility/program/exit">`std::exit`</DocLink>) are not <DocLink dest="/cpp/language/exceptions/catch">caught</DocLink> by it.
9089

9190
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:
9291

@@ -187,6 +186,6 @@ The following behavior-changing defect reports were applied retroactively to pre
187186
188187
<DescList>
189188
<Desc>
190-
<DocLink slot="item" dest="/c/language/main_function"> C documentation</DocLink> for <span> **main function** </span>
189+
<DocLink slot="item" dest="/c/language/basic_concepts/main_function"> C documentation</DocLink> for <span> **main function** </span>
191190
</Desc>
192191
</DescList>

0 commit comments

Comments
 (0)