From 727b8bc5b97972903bd7dff9e989ea5466977469 Mon Sep 17 00:00:00 2001 From: Sudrut Date: Thu, 18 Dec 2025 15:57:08 +0800 Subject: [PATCH 1/5] feat: migrate cpp/language/basic_concepts/definition --- .../language/basic_concepts/definition.mdx | 377 ++++++++++++++++++ 1 file changed, 377 insertions(+) create mode 100644 src/content/docs/cpp/language/basic_concepts/definition.mdx diff --git a/src/content/docs/cpp/language/basic_concepts/definition.mdx b/src/content/docs/cpp/language/basic_concepts/definition.mdx new file mode 100644 index 0000000..3b8df25 --- /dev/null +++ b/src/content/docs/cpp/language/basic_concepts/definition.mdx @@ -0,0 +1,377 @@ +--- +title: Definitions and ODR +--- + +import { Aside } from "@astrojs/starlight/components"; +import { DR, DRList } from "@components/defect-report"; +import { Revision, RevisionBlock } from "@components/revision" +import DocLink from "@components/DocLink.astro" + +_Definitions_ are declarations that fully define the entity introduced by the declaration. Every declaration is a definition, except for the following: + +- A function declaration without a function body: + +```cpp +int f(int); // declares, but does not define f +``` + +- Any declaration with an `extern` storage class specifier or with a language linkage specifier (such as `extern "C"`) without an initializer: + +```cpp +extern const int a; // declares, but does not define a +extern const int b = 1; // defines b +``` + +- Declaration of a non-inline static data member inside a class definition: + +```cpp +struct S { + int n; // defines S::n + static int i; // declares, but does not define S::i + inline static int x; // defines S::x +}; // defines S + +int S::i; // defines S::i +``` + + + - Namespace scope declaration of a static data member that was defined within the class with the constexpr specifier: + + ```cpp + struct S { + static constexpr int x = 42; // implicitly inline, defines S::x + }; + + constexpr int S::x; // declares S::x, not a redefinition + ``` + + +- Declaration of a class name (by forward declaration or by the use of the elaborated type specifier in another declaration): + +```cpp +struct S; // declares, but does not define S + +class Y f(class T p); // declares, but does not define Y and T (and also f and p) +``` + + + - An opaque declaration of an enumeration: + + ```cpp + enum Color : int; // declares, but does not define Color + ``` + + +- Declaration of a template parameter: + +```cpp +template // declares, but does not define T +``` + +- A parameter declaration in a function declaration that isn't a definition: + +```cpp +int f(int x); // declares, but does not define f and x + +int f(int x) { // defines f and x + return x + a; +} +``` + +- A typedef declaration: + +```cpp +typedef S S2; // declares, but does not define S2 (S may be incomplete) +``` + + + - An alias-declaration: + + ```cpp + using S2 = S; // declares, but does not define S2 (S may be incomplete) + ``` + + +- A using-declaration: + +```cpp +using N::d; // declares, but does not define d +``` + + + - Declaration of a deduction guide (does not define any entities) + + + + - A static_assert declaration (does not define any entities) + - An attribute declaration (does not define any entities) + + +- An empty declaration (does not define any entities) +- A using-directive (does not define any entities) + + + - An explicit instantiation declaration (an "extern template"): + + ```cpp + extern template + f; // declares, but does not define f + ``` + + +- An explicit specialization whose declaration is not a definition: + +```cpp +template<> +struct A; // declares, but does not define A +``` + +An asm declaration does not define any entities, but it is classified as a definition. + +Where necessary, the compiler may implicitly define the default constructor, copy constructor, move constructor, copy assignment operator, move assignment operator, and the destructor. + +If the definition of any object results in an object of incomplete type or abstract class type, the program is ill-formed. + +## One Definition Rule + +Only one definition of any variable, function, class type, enumeration type, concept or template is allowed in any one translation unit (some of these may have multiple declarations, but only one definition is allowed). + +One and only one definition of every non-inline function or variable that is _odr-used_ (see below) is required to appear in the entire program (including any standard and user-defined libraries). The compiler is not required to diagnose this violation, but the behavior of the program that violates it is undefined. + +For an inline function or inline variable, a definition is required in every translation unit where it is _odr-used_. + +For a class, a definition is required wherever the class is used in a way that requires it to be complete. + +There can be more than one definition in a program of each of the following: class type, enumeration type, inline function, inline variable, templated entity (template or member of template, but not full template specialization), as long as all following conditions are satisfied: + +- Each definition appears in a different translation unit. + + + - The definitions are not attached to a named module. + + +- Each definition consists of the same sequence of tokens (typically, appears in the same header). +- Name lookup from within each definition finds the same entities (after overload resolution), except that: + - Constants with internal or no linkage may refer to different objects as long as they are not odr-used and have the same values in every definition. + + + - Lambda expressions that are not in a default argument or a default template argument are uniquely identified by the sequence of tokens used to define them. + + +- Overloaded operators, including conversion, allocation, and deallocation functions refer to the same function from each definition (unless referring to one defined within the definition). +- Corresponding entities have the same language linkage in each definition (e.g. the include file is not inside an `extern "C"` block). +- If a const object is constant-initialized in any of the definitions, it is constant-initialized in each definition. +- The rules above apply to every default argument used in each definition. +- If the definition is for a class with an implicitly-declared constructor, every translation unit where it is odr-used must call the same constructor for the base and members. + + + - If the definition is for a class with a defaulted three-way comparison, every translation unit where it is odr-used must call the same comparison operator for the base and members. + + +- If the definition is for a template, then all these requirements apply to both names at the point of definition and dependent names at the point of instantiation. + +If all these requirements are satisfied, the program behaves as if there is only one definition in the entire program. Otherwise, the program is ill-formed, no diagnostic required. + +Note: in C, there is no program-wide ODR for types, and even extern declarations of the same variable in different translation units may have different types as long as they are compatible. In C++, the source-code tokens used in declarations of the same type must be the same as described above: if one .cpp file defines `struct S { int x; };` and the other .cpp file defines `struct S { int y; }`;, the behavior of the program that links them together is undefined. This is usually resolved with unnamed namespaces. + +### Naming an entity + +A variable is _named_ by an expression if the expression is an identifier expression that denotes it. + +A function is _named_ by an expression or conversion in following cases: + +- A function whose name appears as an expression or conversion (including named function, overloaded operator, user-defined conversion, user-defined placement forms of operator new, non-default initialization) is named by that expression if it is selected by overload resolution, except when it is an unqualified pure virtual member function or a pointer-to-member to a pure virtual function. +- An allocation or deallocation function for a class is named by a new expression appearing in an expression. +- A deallocation function for a class is named by a delete expression appearing in an expression. +- A constructor selected to copy or move an object is considered to be named by the expression or conversion even if copy elision takes place. Using a prvalue in some contexts does not copy or move an object, see mandatory elision. + +A potentially evaluated expression or conversion odr-uses a function if it names it. + + + A potentially constant evaluated expression or conversion that names a constexpr function makes it needed for constant evaluation, which triggers definition of a defaulted function or instantiation of a function template specialization, even if the expression is unevaluated. + + +### Potential results + +The set of _potential results_ of an expression `E` is a (possibly empty) set of identifier expressions that appear within `E`, combined as follows: + +- If `E` is an identifier expression, the expression E is its only potential result. +- If `E` is a subscript expression (`E1[E2]`) where one of the operands is an array, the potential results of that operand is included in the set. +- If `E` is a class member access expression of the form `E1.E2` or `E1.template E2` naming a non-static data member, the potential results of `E1` is included in the set. +- If `E` is a class member access expression naming a static data member, the identifier expression designating the data member is included in the set. +- If `E` is a pointer-to-member access expression of the form `E1.*E2` or `E1.*template E2` whose second operand is a constant expression, the potential results of E1 are included in the set. +- If `E` is an expression in parentheses (`(E1)`), the potential results of E1 are included in the set. +- If `E` is a glvalue conditional expression (`E1 ? E2 : E3`, where `E2` and `E3` are glvalues), the union of the potential results of `E2` and `E3` are both included in the set. +- If `E` is a comma expression (`E1, E2`), the potential results of `E2` are in the set of potential results. +- Otherwise, the set is empty. + +### ODR-use (informal definition) + + + +An object is odr-used if its value is read (unless it is a compile time constant) or written, its address is taken, or a reference is bound to it, + +A reference is odr-used if it is used and its referent is not known at compile time, + +A function is odr-used if a function call to it is made or its address is taken. + +If an entity is odr-used, its definition must exist somewhere in the program; a violation of that is usually a link-time error. + +```cpp +struct S { + static const int x = 0; // static data member + // a definition outside of class is required if it is odr-used +}; + +const int& f(const int& r); + +int n = b ? (1, S::x) // S::x is not odr-used here + : f(S::x); // S::x is odr-used here: a definition is required +``` + +### ODR-use (formal definition) + +A variable `x` that is named by a potentially-evaluated expression `expr` that appears at a point P is odr-used by `expr`, unless any of the following conditions is satisfied: + +- `x` is a reference that is usable in constant expressions at P. +- `x` is not a reference and `expr` is an element of the set of potential results of an expression `E`, and any of the following conditions is satisfied: + - `E` is a discarded-value expression, and no lvalue-to-rvalue conversion is applied to it. + - `x` is a non-volatile object that is usable in constant expressions at P and has no mutable subobjects, and any of the following conditions is satisfied: + + - `E` is a class member access expression naming a non-static data member of reference type and whose object expression has non-volatile-qualified type. + + - `E` has non-volatile-qualified non-class type, and the lvalue-to-rvalue conversion is applied to it. + +```cpp +struct S { static const int x = 1; }; // applying lvalue-to-rvalue conversion + // to S::x yields a constant expression + +int f() { + S::x; // discarded-value expression does not odr-use S::x + + return S::x; // expression where lvalue-to-rvalue conversion + // applies does not odr-use S::x +} +``` + +`*this` is odr-used if `this` appears as a potentially-evaluated expression (including the implicit `this` in a non-static member function call expression). + + + A structured binding is odr-used if it appears as a potentially-evaluated expression. + + +A function is odr-used in following cases: + +- A function is odr-used if it is named by (see below) a potentially-evaluated expression or conversion. +- A virtual member function is odr-used if it is not a pure virtual member function (addresses of virtual member functions are required to construct the vtable). +- A non-placement allocation or deallocation function for a class is odr-used by the definition of a constructor of that class. +- A non-placement deallocation function for a class is odr-used by the definition of the destructor of that class, or by being selected by the lookup at the point of definition of a virtual destructor. +- An assignment operator in a class T that is a member or base of another class U is odr-used by an implicitly-defined copy-assignment or move-assignment functions of U. +- A constructor (including default constructors) for a class is odr-used by the initialization that selects it. +- A destructor for a class is odr-used if it is potentially invoked. + + + +## Defect reports + + + + + a deallocation function for a polymorphic class might be odr-used even if there were no relevant new or delete expressions in the program + + + supplemented the odr-use cases to cover constructors and destructors + + + + + an entity could have definitions with different language linkages + + + the behavior is undefined in this case + + + + + reference variables which satisfy the requirements for appearing in a constant expression were odr-used even if the lvalue-to-rvalue conversion is immediately applied + + + they are not odr-used in this case + + + + + taking address of a pure virtual function odr-used it + + + the function is not odr-used + + + + + constant objects that are immediately lvalue-to-rvalue converted in potentially-evaluated expressions were odr-used + + + they are not odr-used + + + + + array subscript expressions did not propagate potential results + + + they propagate + + + + + it was unclear whether a const object that is only constant-initialized in part of its definitions violates ODR + + + ODR is not violated; the object is constant-initialized in this case + + + + + lambda expressions in different translation units could never have the same closure type + + + the closure type can be the same under one definition rule + + + + + a static data member was not a potential result of a member access expression accessing it + + + it is + + + + + a variable template could not have multiple definitions in a program + + + it can + + + + +## References + +- C++23 standard (ISO/IEC 14882:2024): + - 6.3 One definition rule [basic.def.odr] +- C++20 standard (ISO/IEC 14882:2020): + - 6.3 One definition rule [basic.def.odr] +- C++17 standard (ISO/IEC 14882:2017): + - 6.2 One definition rule [basic.def.odr] +- C++14 standard (ISO/IEC 14882:2014): + - 3.2 One definition rule [basic.def.odr] +- C++11 standard (ISO/IEC 14882:2011): + - 3.2 One definition rule [basic.def.odr] +- C++03 standard (ISO/IEC 14882:2003): + - 3.2 One definition rule [basic.def.odr] +- C++98 standard (ISO/IEC 14882:1998): + - 3.2 One definition rule [basic.def.odr] \ No newline at end of file From 90b0b52bfddbd66a59df87468e5390ce1b6e8175 Mon Sep 17 00:00:00 2001 From: Sudrut Date: Thu, 18 Dec 2025 16:28:38 +0800 Subject: [PATCH 2/5] feat: add component `Incomplete` --- src/components/Incomplete.astro | 13 +++++++++++++ src/components/index.ts | 2 ++ .../docs/cpp/language/basic_concepts/definition.mdx | 10 ++++------ 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 src/components/Incomplete.astro diff --git a/src/components/Incomplete.astro b/src/components/Incomplete.astro new file mode 100644 index 0000000..eeed100 --- /dev/null +++ b/src/components/Incomplete.astro @@ -0,0 +1,13 @@ +--- +import { Aside } from "@astrojs/starlight/components"; + +interface Props { + reason: string; +} + +const { reason } = Astro.props; +--- + + diff --git a/src/components/index.ts b/src/components/index.ts index 8fddb90..7455b1a 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -13,6 +13,7 @@ import { DR, DRList } from "@components/defect-report"; import { Revision, RevisionBlock } from "@components/revision"; import AutoCollapse from "@components/AutoCollapse.astro"; import FlexTable from "@components/FlexTable.astro"; +import Incomplete from "@components/Incomplete.astro"; import WG21PaperLink from "@components/WG21PaperLink.astro"; export { @@ -35,5 +36,6 @@ export { RevisionBlock, AutoCollapse, FlexTable, + Incomplete, WG21PaperLink, }; diff --git a/src/content/docs/cpp/language/basic_concepts/definition.mdx b/src/content/docs/cpp/language/basic_concepts/definition.mdx index 3b8df25..7a99513 100644 --- a/src/content/docs/cpp/language/basic_concepts/definition.mdx +++ b/src/content/docs/cpp/language/basic_concepts/definition.mdx @@ -2,10 +2,10 @@ title: Definitions and ODR --- -import { Aside } from "@astrojs/starlight/components"; import { DR, DRList } from "@components/defect-report"; -import { Revision, RevisionBlock } from "@components/revision" -import DocLink from "@components/DocLink.astro" +import { Revision, RevisionBlock } from "@components/revision"; +import DocLink from "@components/DocLink.astro"; +import Incomplete from "@components/Incomplete.astro"; _Definitions_ are declarations that fully define the entity introduced by the declaration. Every declaration is a definition, except for the following: @@ -270,9 +270,7 @@ A function is odr-used in following cases: - A constructor (including default constructors) for a class is odr-used by the initialization that selects it. - A destructor for a class is odr-used if it is potentially invoked. - + ## Defect reports From 7a3e4e11a6d8a86c476f0ff4789c5ae769ae4bf7 Mon Sep 17 00:00:00 2001 From: Sudrut Date: Thu, 18 Dec 2025 16:46:17 +0800 Subject: [PATCH 3/5] feat: update docs for `Incomplete` --- .../development/guide/component-docs-for-llm.mdx | 16 ++++++++++++++++ .../docs/development/guide/doc-everything.mdx | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/content/docs/development/guide/component-docs-for-llm.mdx b/src/content/docs/development/guide/component-docs-for-llm.mdx index 81d47a5..85c11c8 100644 --- a/src/content/docs/development/guide/component-docs-for-llm.mdx +++ b/src/content/docs/development/guide/component-docs-for-llm.mdx @@ -403,6 +403,22 @@ The `DR` component renders a single defect report. See above. +## Incomplete + +The `Incomplete` component marks a section to be incomplete. + +#### Props + +- `reason` (required): The reason why the section is incomplete. + +#### Usage + + +import Incomplete from "@components/Incomplete.astro" + + + + ## Missing Content ### Missing diff --git a/src/content/docs/development/guide/doc-everything.mdx b/src/content/docs/development/guide/doc-everything.mdx index 628ec72..ce15295 100644 --- a/src/content/docs/development/guide/doc-everything.mdx +++ b/src/content/docs/development/guide/doc-everything.mdx @@ -12,6 +12,7 @@ import { Desc, DescList } from "@components/desc-list"; import DocLink from "@components/DocLink.astro"; import { DR, DRList } from "@components/defect-report"; import { FeatureTestMacro, FeatureTestMacroValue } from "@components/feature-test-macro"; +import Incomplete from "@components/Incomplete.astro"; import Missing from "@components/Missing.astro"; import NamedReq from "@components/NamedReq.astro" import { ParamDoc, ParamDocList } from "@components/param-doc"; @@ -538,6 +539,20 @@ import { DR, DRList } from "@components/defect-report"; +## Incomplete Sections + +The `Incomplete` component marks a section to be incomplete. + +```mdx +import { Incomplete } from "@components/Incomplete.astro"; + + +``` + + + + + ## Missing Content The `Missing` component could be used for marking content that is still missing. From fbbf4f141b1f4e31d48d0d079ceb311c8a7df27e Mon Sep 17 00:00:00 2001 From: Sudrut Date: Thu, 18 Dec 2025 16:53:39 +0800 Subject: [PATCH 4/5] fix: correct links in cpp/language/basic_concepts/definition --- .../language/basic_concepts/definition.mdx | 76 +++++++++---------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/content/docs/cpp/language/basic_concepts/definition.mdx b/src/content/docs/cpp/language/basic_concepts/definition.mdx index 7a99513..7ef2c4d 100644 --- a/src/content/docs/cpp/language/basic_concepts/definition.mdx +++ b/src/content/docs/cpp/language/basic_concepts/definition.mdx @@ -15,14 +15,14 @@ _Definitions_ are declarationsstorage class specifier or with a language linkage specifier (such as `extern "C"`) without an initializer: +- Any declaration with an `extern` storage class specifier or with a language linkage specifier (such as `extern "C"`) without an initializer: ```cpp extern const int a; // declares, but does not define a extern const int b = 1; // defines b ``` -- Declaration of a non-inline static data member inside a class definition: +- Declaration of a non-inline static data member inside a class definition: ```cpp struct S { @@ -35,7 +35,7 @@ int S::i; // defines S::i ``` - - Namespace scope declaration of a static data member that was defined within the class with the constexpr specifier: + - Namespace scope declaration of a static data member that was defined within the class with the constexpr specifier: ```cpp struct S { @@ -46,7 +46,7 @@ int S::i; // defines S::i ``` -- Declaration of a class name (by forward declaration or by the use of the elaborated type specifier in another declaration): +- Declaration of a class name (by forward declaration or by the use of the elaborated type specifier in another declaration): ```cpp struct S; // declares, but does not define S @@ -55,14 +55,14 @@ class Y f(class T p); // declares, but does not define Y and T (and also f and p ``` - - An opaque declaration of an enumeration: + - An opaque declaration of an enumeration: ```cpp enum Color : int; // declares, but does not define Color ``` -- Declaration of a template parameter: +- Declaration of a template parameter: ```cpp template // declares, but does not define T @@ -78,40 +78,40 @@ int f(int x) { // defines f and x } ``` -- A typedef declaration: +- A typedef declaration: ```cpp typedef S S2; // declares, but does not define S2 (S may be incomplete) ``` - - An alias-declaration: + - An alias-declaration: ```cpp using S2 = S; // declares, but does not define S2 (S may be incomplete) ``` -- A using-declaration: +- A using-declaration: ```cpp using N::d; // declares, but does not define d ``` - - Declaration of a deduction guide (does not define any entities) + - Declaration of a deduction guide (does not define any entities) - - A static_assert declaration (does not define any entities) + - A static_assert declaration (does not define any entities) - An attribute declaration (does not define any entities) - An empty declaration (does not define any entities) -- A using-directive (does not define any entities) +- A using-directive (does not define any entities) - - An explicit instantiation declaration (an "extern template"): + - An explicit instantiation declaration (an "extern template"): ```cpp extern template @@ -119,60 +119,60 @@ using N::d; // declares, but does not define d ``` -- An explicit specialization whose declaration is not a definition: +- An explicit specialization whose declaration is not a definition: ```cpp template<> struct A; // declares, but does not define A ``` -An asm declaration does not define any entities, but it is classified as a definition. +An asm declaration does not define any entities, but it is classified as a definition. -Where necessary, the compiler may implicitly define the default constructor, copy constructor, move constructor, copy assignment operator, move assignment operator, and the destructor. +Where necessary, the compiler may implicitly define the default constructor, copy constructor, move constructor, copy assignment operator, move assignment operator, and the destructor. -If the definition of any object results in an object of incomplete type or abstract class type, the program is ill-formed. +If the definition of any object results in an object of incomplete type or abstract class type, the program is ill-formed. ## One Definition Rule -Only one definition of any variable, function, class type, enumeration type, concept or template is allowed in any one translation unit (some of these may have multiple declarations, but only one definition is allowed). +Only one definition of any variable, function, class type, enumeration type, concept or template is allowed in any one translation unit (some of these may have multiple declarations, but only one definition is allowed). -One and only one definition of every non-inline function or variable that is _odr-used_ (see below) is required to appear in the entire program (including any standard and user-defined libraries). The compiler is not required to diagnose this violation, but the behavior of the program that violates it is undefined. +One and only one definition of every non-inline function or variable that is _odr-used_ (see below) is required to appear in the entire program (including any standard and user-defined libraries). The compiler is not required to diagnose this violation, but the behavior of the program that violates it is undefined. For an inline function or inline variable, a definition is required in every translation unit where it is _odr-used_. -For a class, a definition is required wherever the class is used in a way that requires it to be complete. +For a class, a definition is required wherever the class is used in a way that requires it to be complete. -There can be more than one definition in a program of each of the following: class type, enumeration type, inline function, inline variable, templated entity (template or member of template, but not full template specialization), as long as all following conditions are satisfied: +There can be more than one definition in a program of each of the following: class type, enumeration type, inline function, inline variable, templated entity (template or member of template, but not full template specialization), as long as all following conditions are satisfied: - Each definition appears in a different translation unit. - - The definitions are not attached to a named module. + - The definitions are not attached to a named module. -- Each definition consists of the same sequence of tokens (typically, appears in the same header). -- Name lookup from within each definition finds the same entities (after overload resolution), except that: +- Each definition consists of the same sequence of tokens (typically, appears in the same header). +- Name lookup from within each definition finds the same entities (after overload resolution), except that: - Constants with internal or no linkage may refer to different objects as long as they are not odr-used and have the same values in every definition. - - Lambda expressions that are not in a default argument or a default template argument are uniquely identified by the sequence of tokens used to define them. + - Lambda expressions that are not in a default argument or a default template argument are uniquely identified by the sequence of tokens used to define them. - Overloaded operators, including conversion, allocation, and deallocation functions refer to the same function from each definition (unless referring to one defined within the definition). - Corresponding entities have the same language linkage in each definition (e.g. the include file is not inside an `extern "C"` block). -- If a const object is constant-initialized in any of the definitions, it is constant-initialized in each definition. +- If a const object is constant-initialized in any of the definitions, it is constant-initialized in each definition. - The rules above apply to every default argument used in each definition. - If the definition is for a class with an implicitly-declared constructor, every translation unit where it is odr-used must call the same constructor for the base and members. - - If the definition is for a class with a defaulted three-way comparison, every translation unit where it is odr-used must call the same comparison operator for the base and members. + - If the definition is for a class with a defaulted three-way comparison, every translation unit where it is odr-used must call the same comparison operator for the base and members. - If the definition is for a template, then all these requirements apply to both names at the point of definition and dependent names at the point of instantiation. If all these requirements are satisfied, the program behaves as if there is only one definition in the entire program. Otherwise, the program is ill-formed, no diagnostic required. -Note: in C, there is no program-wide ODR for types, and even extern declarations of the same variable in different translation units may have different types as long as they are compatible. In C++, the source-code tokens used in declarations of the same type must be the same as described above: if one .cpp file defines `struct S { int x; };` and the other .cpp file defines `struct S { int y; }`;, the behavior of the program that links them together is undefined. This is usually resolved with unnamed namespaces. +Note: in C, there is no program-wide ODR for types, and even extern declarations of the same variable in different translation units may have different types as long as they are compatible. In C++, the source-code tokens used in declarations of the same type must be the same as described above: if one .cpp file defines `struct S { int x; };` and the other .cpp file defines `struct S { int y; }`;, the behavior of the program that links them together is undefined. This is usually resolved with unnamed namespaces. ### Naming an entity @@ -180,15 +180,15 @@ A variable is _named_ by an expression if the expression is an identifier expres A function is _named_ by an expression or conversion in following cases: -- A function whose name appears as an expression or conversion (including named function, overloaded operator, user-defined conversion, user-defined placement forms of operator new, non-default initialization) is named by that expression if it is selected by overload resolution, except when it is an unqualified pure virtual member function or a pointer-to-member to a pure virtual function. -- An allocation or deallocation function for a class is named by a new expression appearing in an expression. -- A deallocation function for a class is named by a delete expression appearing in an expression. -- A constructor selected to copy or move an object is considered to be named by the expression or conversion even if copy elision takes place. Using a prvalue in some contexts does not copy or move an object, see mandatory elision. +- A function whose name appears as an expression or conversion (including named function, overloaded operator, user-defined conversion, user-defined placement forms of operator new, non-default initialization) is named by that expression if it is selected by overload resolution, except when it is an unqualified pure virtual member function or a pointer-to-member to a pure virtual function. +- An allocation or deallocation function for a class is named by a new expression appearing in an expression. +- A deallocation function for a class is named by a delete expression appearing in an expression. +- A constructor selected to copy or move an object is considered to be named by the expression or conversion even if copy elision takes place. Using a prvalue in some contexts does not copy or move an object, see mandatory elision. A potentially evaluated expression or conversion odr-uses a function if it names it. - A potentially constant evaluated expression or conversion that names a constexpr function makes it needed for constant evaluation, which triggers definition of a defaulted function or instantiation of a function template specialization, even if the expression is unevaluated. + A potentially constant evaluated expression or conversion that names a constexpr function makes it needed for constant evaluation, which triggers definition of a defaulted function or instantiation of a function template specialization, even if the expression is unevaluated. ### Potential results @@ -233,12 +233,12 @@ int n = b ? (1, S::x) // S::x is not odr-used here A variable `x` that is named by a potentially-evaluated expression `expr` that appears at a point P is odr-used by `expr`, unless any of the following conditions is satisfied: -- `x` is a reference that is usable in constant expressions at P. +- `x` is a reference that is usable in constant expressions at P. - `x` is not a reference and `expr` is an element of the set of potential results of an expression `E`, and any of the following conditions is satisfied: - `E` is a discarded-value expression, and no lvalue-to-rvalue conversion is applied to it. - `x` is a non-volatile object that is usable in constant expressions at P and has no mutable subobjects, and any of the following conditions is satisfied: - - `E` is a class member access expression naming a non-static data member of reference type and whose object expression has non-volatile-qualified type. + - `E` is a class member access expression naming a non-static data member of reference type and whose object expression has non-volatile-qualified type. - `E` has non-volatile-qualified non-class type, and the lvalue-to-rvalue conversion is applied to it. @@ -257,18 +257,18 @@ int f() { `*this` is odr-used if `this` appears as a potentially-evaluated expression (including the implicit `this` in a non-static member function call expression). - A structured binding is odr-used if it appears as a potentially-evaluated expression. + A structured binding is odr-used if it appears as a potentially-evaluated expression. A function is odr-used in following cases: - A function is odr-used if it is named by (see below) a potentially-evaluated expression or conversion. -- A virtual member function is odr-used if it is not a pure virtual member function (addresses of virtual member functions are required to construct the vtable). +- A virtual member function is odr-used if it is not a pure virtual member function (addresses of virtual member functions are required to construct the vtable). - A non-placement allocation or deallocation function for a class is odr-used by the definition of a constructor of that class. - A non-placement deallocation function for a class is odr-used by the definition of the destructor of that class, or by being selected by the lookup at the point of definition of a virtual destructor. - An assignment operator in a class T that is a member or base of another class U is odr-used by an implicitly-defined copy-assignment or move-assignment functions of U. - A constructor (including default constructors) for a class is odr-used by the initialization that selects it. -- A destructor for a class is odr-used if it is potentially invoked. +- A destructor for a class is odr-used if it is potentially invoked. From a3a1e3301d49da5b65dc1da38fc13dd853c2a31c Mon Sep 17 00:00:00 2001 From: Sudrut Date: Thu, 18 Dec 2025 17:06:54 +0800 Subject: [PATCH 5/5] fix: emphasize undefined behavior --- .../docs/cpp/language/basic_concepts/definition.mdx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/content/docs/cpp/language/basic_concepts/definition.mdx b/src/content/docs/cpp/language/basic_concepts/definition.mdx index 7ef2c4d..cb0d40b 100644 --- a/src/content/docs/cpp/language/basic_concepts/definition.mdx +++ b/src/content/docs/cpp/language/basic_concepts/definition.mdx @@ -4,6 +4,7 @@ title: Definitions and ODR import { DR, DRList } from "@components/defect-report"; import { Revision, RevisionBlock } from "@components/revision"; +import Behavior from "@components/Behavior.astro"; import DocLink from "@components/DocLink.astro"; import Incomplete from "@components/Incomplete.astro"; @@ -130,13 +131,13 @@ An asm declaration does Where necessary, the compiler may implicitly define the default constructor, copy constructor, move constructor, copy assignment operator, move assignment operator, and the destructor. -If the definition of any object results in an object of incomplete type or abstract class type, the program is ill-formed. +If the definition of any object results in an object of incomplete type or abstract class type, the program is ill-formed. ## One Definition Rule Only one definition of any variable, function, class type, enumeration type, concept or template is allowed in any one translation unit (some of these may have multiple declarations, but only one definition is allowed). -One and only one definition of every non-inline function or variable that is _odr-used_ (see below) is required to appear in the entire program (including any standard and user-defined libraries). The compiler is not required to diagnose this violation, but the behavior of the program that violates it is undefined. +One and only one definition of every non-inline function or variable that is _odr-used_ (see below) is required to appear in the entire program (including any standard and user-defined libraries). The compiler is not required to diagnose this violation, but the behavior of the program that violates it is undefined. For an inline function or inline variable, a definition is required in every translation unit where it is _odr-used_. @@ -170,9 +171,9 @@ There can be more than one definition in a program of each of the following: cla - If the definition is for a template, then all these requirements apply to both names at the point of definition and dependent names at the point of instantiation. -If all these requirements are satisfied, the program behaves as if there is only one definition in the entire program. Otherwise, the program is ill-formed, no diagnostic required. +If all these requirements are satisfied, the program behaves as if there is only one definition in the entire program. Otherwise, the program is ill-formed, no diagnostic required. -Note: in C, there is no program-wide ODR for types, and even extern declarations of the same variable in different translation units may have different types as long as they are compatible. In C++, the source-code tokens used in declarations of the same type must be the same as described above: if one .cpp file defines `struct S { int x; };` and the other .cpp file defines `struct S { int y; }`;, the behavior of the program that links them together is undefined. This is usually resolved with unnamed namespaces. +Note: in C, there is no program-wide ODR for types, and even extern declarations of the same variable in different translation units may have different types as long as they are compatible. In C++, the source-code tokens used in declarations of the same type must be the same as described above: if one .cpp file defines `struct S { int x; };` and the other .cpp file defines `struct S { int y; }`;, the behavior of the program that links them together is undefined. This is usually resolved with unnamed namespaces. ### Naming an entity