|
| 1 | +--- |
| 2 | +title: "C++ named requirements: DefaultConstructible" |
| 3 | +cppdoc: |
| 4 | + revision: |
| 5 | + since: C++11 |
| 6 | +--- |
| 7 | + |
| 8 | +import { DR, DRList } from "@components/defect-report"; |
| 9 | +import { Desc, DescList } from "@components/desc-list"; |
| 10 | + |
| 11 | +Specifies that an instance of the type can be default constructed. |
| 12 | + |
| 13 | + |
| 14 | +## Requirements |
| 15 | +The type `T` satisfies _DefaultConstructible_ if all following statements and expressions are valid and have their specified effects: |
| 16 | + |
| 17 | +Given |
| 18 | + |
| 19 | +- `u`, an expression of type `T`. |
| 20 | + |
| 21 | +- `u`, an lvalue expression of type `Key`. |
| 22 | + |
| 23 | +|Expression/Statement| Postcondition| |
| 24 | +|---|---| |
| 25 | +|`T u;`| The object `u` is default-initialized.| |
| 26 | +|`T u{};`| The object `u` is value-initialized or aggregate-initialized.| |
| 27 | +|`T()`| All resources owned by `u` are reclaimed, no exceptions are thrown.| |
| 28 | +|`T{}`| A temporary object of type `T` is value-initialized or aggregate-initialized.| |
| 29 | + |
| 30 | +## Notes |
| 31 | + |
| 32 | +For objects of non-aggregate class type, a public default constructor must be defined (either user-defined or implicitly defined) to satisfy _DefaultConstructible_. |
| 33 | + |
| 34 | +Non-const objects of non-class object type are always _DefaultConstructible_. |
| 35 | + |
| 36 | +Const non-class types are not _DefaultConstructible_. |
| 37 | + |
| 38 | +Const aggregate types are not _DefaultConstructible_ if any of their members is an object of non-class type. |
| 39 | + |
| 40 | +Non-object types (function types, reference types, and the (possibly cv-qualified) type void) as well as the const non-object types are never _DefaultConstructible_. |
| 41 | + |
| 42 | +# Defect reports |
| 43 | + |
| 44 | +The following behavior-changing defect reports were applied retroactively to previously published C++ standards. |
| 45 | + |
| 46 | +<DRList> |
| 47 | + <DR kind="lwg 724" id={1} std="C++98"> |
| 48 | + <Fragment slot="behavior-published"> |
| 49 | + the requirements of _DefaultConstructible_ were missing |
| 50 | + </Fragment> |
| 51 | + <Fragment slot="correct-behavior"> |
| 52 | + added |
| 53 | + </Fragment> |
| 54 | + </DR> |
| 55 | + <DR kind="lwg 2170" id={1} std="C++98"> |
| 56 | + <Fragment slot="behavior-published"> |
| 57 | + initialzing an object of a _DefaultConstructible_ type with an |
| 58 | +empty initializer could only result in value-initialization |
| 59 | + </Fragment> |
| 60 | + <Fragment slot="correct-behavior"> |
| 61 | + can also lead to aggregate-initialization |
| 62 | + </Fragment> |
| 63 | + </DR> |
| 64 | +</DRList> |
| 65 | +## See also |
| 66 | + |
| 67 | +<DescList> |
| 68 | + <Desc kind="class template"> |
| 69 | + <Fragment slot="item"> |
| 70 | + <RevisionBlock noborder since="C++11" vertical> |
| 71 | + <Missing> `std::is_default_constructible` </Missing> |
| 72 | + </RevisionBlock> |
| 73 | + <RevisionBlock noborder since="C++11" vertical> |
| 74 | + <Missing> `std::is_trivially_default_constructible` </Missing> |
| 75 | + </RevisionBlock> |
| 76 | + <RevisionBlock noborder since="C++11" vertical> |
| 77 | + <Missing> `std::is_nothrow_default_constructible` </Missing> |
| 78 | + </RevisionBlock> |
| 79 | + </Fragment> |
| 80 | + checks if a type has a default constructor |
| 81 | + </Desc> |
| 82 | + <Desc kind="concept"> |
| 83 | + <Fragment slot="item"> |
| 84 | + <RevisionBlock noborder since="C++20" vertical> |
| 85 | + <Missing> `default_initializable` </Missing> |
| 86 | + </RevisionBlock> |
| 87 | + </Fragment> |
| 88 | + specifies that an object of a type can be default constructed |
| 89 | + </Desc> |
| 90 | +</DescList> |
| 91 | + |
0 commit comments