-
Notifications
You must be signed in to change notification settings - Fork 11
update preprocessor documentation #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
momo773510
wants to merge
4
commits into
cppdoc-cc:main
Choose a base branch
from
momo773510:add_doc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
95a439b
update preprocessor documentation
momo773510 a69c304
feat: migrate cpp/templates cpp/exceptions from cppref
momo773510 db6d7de
Merge branch 'main' of https://github.com/momo773510/cppdoc into add_doc
momo773510 b16f791
fix preprocessor doc
momo773510 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| --- | ||
| title: Exceptions | ||
| cppdoc: | ||
| keys: ["cpp.lang.exceptions"] | ||
| --- | ||
|
|
||
| import { Decl, DeclDoc } from "@components/decl-doc"; | ||
| import { Desc, DescList, DocLink } from '@components/index'; | ||
| import { Revision, RevisionBlock } from "@components/revision"; | ||
| import { DR, DRList } from "@components/defect-report"; | ||
| import { ParamDoc, ParamDocList } from "@components/param-doc"; | ||
| import { FeatureTestMacro, FeatureTestMacroValue } from "@components/feature-test-macro"; | ||
|
|
||
| Exception handling provides a way of transferring control and information from some point in the execution of a program to a handler associated with a point previously passed by the execution (in other words, exception handling transfers control up the call stack). | ||
|
|
||
| Evaluating a <DocLink src="cpp/language/throw.html#throw_expressions">`throw` expression</DocLink> will throw an exception. Exceptions can also be thrown in <DocLink src="cpp/language/throw.html#throw_expressions">other contexts</DocLink>. | ||
|
|
||
| In order for an exception to be caught, the `throw` expression has to be inside a <DocLink src="cpp/language/try">`try` block</DocLink>, and the `try` block has to contain a <DocLink src="cpp/language/catch">handler</DocLink> that matches the type of the exception object. | ||
|
|
||
| When declaring a function, the following specification(s) may be provided to limit the types of the exceptions a function may throw: | ||
|
|
||
| - <Revision since="C++17"><DocLink src="cpp/language/cpp/language/except_spec">dynamic exception specifications</DocLink></Revision> | ||
| - <Revision since="C++11"><DocLink src="cpp/language/cpp/language/noexcept_spec">noexcept specifications</DocLink></Revision> | ||
|
|
||
| Errors that arise during exception handling are handled by <DocLink src="cpp/error/terminate">`std::terminate`</DocLink> and <Revision since="C++17"><DocLink src="cpp/error/unexpected">`std::unexpected`</DocLink></Revision>. | ||
|
|
||
| ## Usage | ||
|
|
||
| While `throw` expression can be used to transfer control to an arbitrary block of code up the execution stack, for arbitrary reasons (similar to <DocLink src="cpp/utility/program/longjmp">`std::longjmp`</DocLink>), its intended usage is error handling. | ||
|
|
||
| ### Error handling | ||
|
|
||
| Throwing an exception is used to signal errors from functions, where "errors" are typically limited to only the following[^1] [^2] [^3]: | ||
|
|
||
| 1. Failures to meet the postconditions, such as failing to produce a valid return value object. | ||
| 2. Failures to meet the preconditions of another function that must be called. | ||
| 3. (for non-private member functions) Failures to (re)establish a class invariant. | ||
|
|
||
| In particular, this implies that the failures of constructors (see also <DocLink src="cpp/language/raii">RAII</DocLink>) and most operators should be reported by throwing exceptions. | ||
|
|
||
| In addition, so-called *wide contract* functions use exceptions to indicate unacceptable inputs, for example, <DocLink src="cpp/string/basic_string/at">`std::basic_string::at`</DocLink> has no preconditions, but throws an exception to indicate index out of range. | ||
|
|
||
| ### Exception safety | ||
|
|
||
| After the error condition is reported by a function, additional guarantees may be provided with regards to the state of the program. The following four levels of exception guarantee are generally recognized[^4] [^5] [^6], which are strict supersets of each other: | ||
|
|
||
| 1. *Nothrow (or nofail) exception guarantee* — the function never throws exceptions. Nothrow (errors are reported by other means or concealed) is expected of <DocLink src="cpp/language/destructor">destructors</DocLink> and other functions that may be called during stack unwinding. <Revision since="C++11">The <DocLink src="cpp/language/destructor">destructors</DocLink> are <DocLink src="cpp/language/noexcept">`noexcept`</DocLink> by default.</Revision> Nofail (the function always succeeds) is expected of swaps, <DocLink src="cpp/language/move_constructor">move constructors</DocLink>, and other functions used by those that provide strong exception guarantee. | ||
| 2. *Strong exception guarantee* — If the function throws an exception, the state of the program is rolled back to the state just before the function call (for example, <DocLink src="cpp/container/vector/push_back">`std::vector::push_back`</DocLink>). | ||
| 3. *Basic exception guarantee* — If the function throws an exception, the program is in a valid state. No resources are leaked, and all objects' invariants are intact. | ||
| 4. *No exception guarantee* — If the function throws an exception, the program may not be in a valid state: resource leaks, memory corruption, or other invariant-destroying errors may have occurred. | ||
|
|
||
| Generic components may, in addition, offer *exception-neutral guarantee*: if an exception is thrown from a template parameter (e.g. from the `Compare` function object of <DocLink src="cpp/algorithm/sort">`std::sort`</DocLink> or from the constructor of `T` in <DocLink src="cpp/memory/shared_ptr/make_shared">`std::make_shared`</DocLink>), it is propagated, unchanged, to the caller. | ||
|
|
||
| ## Exception objects | ||
|
|
||
| While objects of any complete type and cv pointers to `void` may be thrown as exception objects, all standard library functions throw unnamed objects by value, and the types of those objects are derived (directly or indirectly) from <DocLink src="cpp/error/exception">`std::exception`</DocLink>. User-defined exceptions usually follow this pattern.[^7] [^8] [^9] | ||
|
|
||
| To avoid unnecessary copying of the exception object and object slicing, the best practice for handlers is to catch by reference.[^10] [^11] [^12] [^13] | ||
|
|
||
| ## Notes | ||
|
|
||
| <FeatureTestMacro name="__cpp_constexpr_exceptions"> | ||
| <FeatureTestMacroValue value="202411L" since="C++26"> | ||
| `constexpr` exceptions | ||
| </FeatureTestMacroValue> | ||
| </FeatureTestMacro> | ||
|
|
||
| ## External links | ||
|
|
||
| [^1]: H. Sutter (2004) [When and How to Use Exceptions](https://www.drdobbs.com/when-and-how-to-use-exceptions/184401836) in Dr. Dobb's | ||
| [^2]: H. Sutter, A. Alexandrescu (2004) "C++ Coding Standards" Item 70 | ||
| [^3]: C++ Core Guidelines [I.10: Use exceptions to signal a failure to perform a required task](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Ri-except) | ||
| [^4]: B. Stroustrup (2000) "The C++ Programming Language" [Appendix E](https://stroustrup.com/3rd_safe.pdf) | ||
| [^5]: H. Sutter (2000) "Exceptional C++" | ||
| [^6]: D. Abrahams (2001) ["Exception Safety in Generic Components"](https://www.boost.org/community/exception_safety.html) | ||
| [^7]: D. Abrahams (2001) ["Error and Exception Handling"](https://www.boost.org/community/error_handling.html) | ||
| [^8]: isocpp.org Super-FAQ ["What should I throw?"](https://isocpp.org/wiki/faq/exceptions#what-to-throw) | ||
| [^9]: C++ Core Guidelines [E.14: Use purpose-designed user-defined types as exceptions (not built-in types)](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Re-exception-types) | ||
| [^10]: C++ Core Guidelines [E.15: Throw by value, catch exceptions from a hierarchy by reference](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Re-exception-ref) | ||
| [^11]: S. Meyers (1996) "More Effective C++" Item 13 | ||
| [^12]: isocpp.org Super-FAQ ["What should I catch?"](https://isocpp.org/wiki/faq/exceptions#what-to-catch) | ||
| [^13]: H. Sutter, A. Alexandrescu (2004) "C++ Coding Standards" Item 73 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| --- | ||
| title: preprocessor | ||
| cppdoc: | ||
| keys: ["cpp.lang.preprocessor"] | ||
| --- | ||
|
|
||
| import { Desc, DescList, DocLink } from '@components/index'; | ||
| import { Revision, RevisionBlock } from "@components/revision"; | ||
| import { DR, DRList } from "@components/defect-report"; | ||
|
|
||
| The preprocessor is executed at <DocLink src="/cpp/language/translation_phases" anchor="Phase_4">translation phase 4</DocLink>, before the compilation. The result of preprocessing is a single file which is then passed to the actual compiler. | ||
|
|
||
| ## Directives | ||
|
|
||
| The preprocessing directives control the behavior of the preprocessor. Each directive occupies one line and has the following format: | ||
|
|
||
| * the `#` character. | ||
| * a sequence of: | ||
| * a standard-defined directive name (listed [below](#Capabilities)) followed by the corresponding arguments, or | ||
| * one or more <DocLink src="/cpp/language/translation_phases" anchor="Phase_3">preprocessing tokens</DocLink> where the beginning token is not a standard-defined directive name, in this case the directive is conditionally-supported with implementation-defined semantics <Revision until="C++23">(e.g. a common non-standard extension is the directive `#warning` which emits a user-defined message during compilation)</Revision>, or | ||
| * nothing, in this case the directive has no effect. | ||
| * a line break. | ||
|
|
||
| <Revision since="C++20">The <DocLink src="/cpp/language/modules">module and import directives</DocLink> are also preprocessing directives.</Revision> | ||
|
|
||
| Preprocessing directives must not come from macro expansion. | ||
|
|
||
| ```cpp | ||
| #define EMPTY | ||
| EMPTY # include <file.h> // not a preprocessing directive | ||
| ``` | ||
|
|
||
| ## Capabilities | ||
|
|
||
| The preprocessor has the source file translation capabilities: | ||
|
|
||
| * **<DocLink src="/cpp/preprocessor/conditional">conditionally</DocLink>** compile parts of source file (controlled by directive `#if`, `#ifdef`, `#ifndef`, `#else`, <Revision since="C++23">`#elif`, `#elifdef`, `#elifndef`</Revision>, and `#endif`). | ||
| * **<DocLink src="/cpp/preprocessor/replace">replace</DocLink>** text macros while possibly concatenating or quoting identifiers (controlled by directives `#define` and `#undef`, and operators `#` and `##`). | ||
| * **<DocLink src="/cpp/preprocessor/include">include</DocLink>** other files (controlled by directive `#include` <Revision since="C++23">and checked with `__has_include` </Revision>). | ||
| * cause an **<DocLink src="/cpp/preprocessor/error">error</DocLink>** <Revision since="C++17">or **<DocLink src="/cpp/preprocessor/error">warning</DocLink>** </Revision> (controlled by directive `#error` <Revision since="C++23">or `#warning` respectively</Revision>). | ||
|
|
||
| The following aspects of the preprocessor can be controlled: | ||
|
|
||
| * **<DocLink src="/cpp/preprocessor/impl">implementation-defined</DocLink>** behavior (controlled by directive `#pragma` <Revision since="C++11">and operator `_Pragma` </Revision>). In addition, some compilers support (to varying degrees) the operator `__pragma` as a *non-standard* extension. | ||
| * **<DocLink src="/cpp/preprocessor/line">file name and line information</DocLink>** available to the preprocessor (controlled by directive `#line`). | ||
|
|
||
| ## Defect reports | ||
|
|
||
| The following behavior-changing defect reports were applied retroactively to previously published C++ standards. | ||
|
|
||
| <DRList> | ||
| <DR kind="cwg" id={2001} std="C++98"> | ||
| <Fragment slot="behavior-published"> | ||
| the behavior of using non-standard-defined directives was not clear | ||
| </Fragment> | ||
| <Fragment slot="correct-behavior"> | ||
| made conditionally-supported | ||
| </Fragment> | ||
| </DR> | ||
| </DRList> | ||
|
|
||
| ## See also | ||
|
|
||
| <DescList> | ||
| <Desc> | ||
| <DocLink slot="item" src="/cpp/preprocessor/replace" anchor="Predefined_macros">C++ documentation</DocLink> for <span>Predefined Macro Symbols</span> | ||
| </Desc> | ||
| <Desc> | ||
| <DocLink slot="item" src="/cpp/symbol_index/macro">C++ documentation</DocLink> for <span>Macro Symbol Index</span> | ||
| </Desc> | ||
| <Desc> | ||
| <DocLink slot="item" src="/c/preprocessor">C documentation</DocLink> for <span>preprocessor</span> | ||
| </Desc> | ||
| </DescList> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be clearer not to say "non-standard extension", because almost nothing can be considered as "standard extension". It would make more sense to say "conforming/conformant extension". But given a large number of extensions are conforming, I think we should only additionally note non-conforming cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe that make sense. but the statement is just a copy from cppreference. i use LLM to replicate it and could not distinguish the better one.