Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/named req/CopyConstructible"

From cppreference.com
< cpp‎ | named req
(+other variants)
(Example: CopyConstructible is false, is_copy_constructible is true)
 
(26 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{cpp/concept/title|CopyConstructible}}
+
{{cpp/named req/title|CopyConstructible}}
{{cpp/concept/sidebar}}
+
{{cpp/named req/navbar}}
  
Specifies that an instance of the type can be copy-constructed (copied).  
+
Specifies that an instance of the type can be copy-constructed from an [[cpp/language/value category|lvalue expression]].
  
 
===Requirements===
 
===Requirements===
 +
The type {{tt|T}} satisfies {{named req/core|CopyConstructible}} if
  
The type must implement the following functions:
+
* The type {{tt|T}} satisfies {{named req|MoveConstructible}}, and
  
{{member | {{dcl small|Type::}}Type | 2=
+
Given
 +
* {{c|v}}, an [[cpp/language/value_category#lvalue|lvalue]] expression of type {{tt|T}} or {{c/core|const T}} or an [[cpp/language/value category#rvalue|rvalue]] expression of type {{c/core|const T}},
 +
* {{c|u}}, an arbitrary identifier.
  
{{ddcl | notes={{mark|One of the variants is sufficient}} | 1=
+
The following expressions must be valid and have their specified effects:
Type::Type( Type& other );
+
{|table class=wikitable
Type::Type( const Type& other );
+
|-
Type::Type( volatile Type& other );
+
!Expression||Post-conditions
Type::Type( const volatile Type& other );
+
|-
 +
|{{c|1=T u = v;}}
 +
|The value of {{c|u}} is equivalent to the value of {{c|v}}.
 +
The value of {{c|v}} is unchanged.
 +
|-
 +
|{{c|T(v)}}
 +
|The value of {{c|T(v)}} is equivalent to the value of {{c|v}}.
 +
The value of {{c|v}} is unchanged.
 +
|}
 +
 
 +
{{rev begin}}
 +
{{rev|until=c++11|
 +
The expression {{c|v.~T()}} also must be valid, and, for lvalue {{c|v}}, the expression {{c|&v}} must have the type {{tt|T*}} or {{c/core|const T*}} and must evaluate to the address of {{c|v}}.
 
}}
 
}}
{{ddcl | Type::Type( const Type& other );}}
+
{{rev end}}
  
[[cpp/language/copy_constructor|Copy constructor]]: constructs an instance of a type with the contents of {{tt|other}}. The internal state of {{tt|other}} is not modified.
+
===Notes===
 +
Until C++11, classes that overloaded {{c/core|operator&}} were not {{named req/core|CopyConstructible}} and thus were not usable in the [[cpp/container|standard library containers]]. This is a design decision in C++98 (instead of a defect, see {{lwg|390}}).
  
The following expressions must have the specified effects:
+
Since C++11, the standard library uses {{lc|std::addressof}} whenever the address of an object is needed.
  
{{tdcl list begin}}
+
{{cot}}
{{tdcl list hitem | Expression | Effects}}
+
 
{{tdcl list item | {{cpp|1=Type a = v;}} | {{tt|a}} is equivalent to {{tt|v}}, where {{tt|rv}} is an instance of {{tt|Type}}. {{tt|v}} must be unchanged.}}
+
Being a {{named req/core|CopyConstructible}} class implies {{lc|std::is_copy_constructible}} but not vice versa since {{lc|std::is_copy_constructible}} will only check for the ability to call the constructor with the correct arguments, and, e.g., not a {{named req|MoveConstructible}} requirement.
{{tdcl list item | {{cpp|Type(v);}} |  a temporary object of type {{tt|Type}} is equivalent to {{tt|v}}, where {{tt|v}} is an instance of {{tt|Type}}. {{tt|v}} must be unchanged. }}
+
 
{{tdcl list end}}
+
{{example
 +
|code=
 +
#include <type_traits>
 +
#include <utility>
 +
 
 +
struct S
 +
{
 +
    S() = default;
 +
    S(S&&) = delete;
 +
    S(const S&) = default;
 +
};
 +
static_assert(std::is_copy_constructible_v<S>);
 +
 
 +
int main()
 +
{
 +
    S s1;
 +
 
 +
    // Class `S` doesn't satisfy MoveConstructible requirement,
 +
    // hence doesn't satisfy CopyConstructible requirement
 +
    [[maybe_unused]] S s2{std::move(s1)}; // ill-formed, use of deleted function
 +
}
 
}}
 
}}
 +
{{cob}}
 +
 +
===References===
 +
{{cot}}
 +
{{ref std c++23}}
 +
{{ref std|section=16.4.4.2|title=Template argument requirements|id=utility.arg.requirements}}
 +
{{ref std end}}
 +
{{cob}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/types/dsc is_copy_constructible}}
 +
{{dsc inc|cpp/concepts/dsc copy_constructible}}
 +
{{dsc end}}
 +
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 22:19, 3 March 2024

 
 
C++ named requirements
 

Specifies that an instance of the type can be copy-constructed from an lvalue expression.

Contents

[edit] Requirements

The type T satisfies CopyConstructible if

Given

  • v, an lvalue expression of type T or const T or an rvalue expression of type const T,
  • u, an arbitrary identifier.

The following expressions must be valid and have their specified effects:

Expression Post-conditions
T u = v; The value of u is equivalent to the value of v.

The value of v is unchanged.

T(v) The value of T(v) is equivalent to the value of v.

The value of v is unchanged.

The expression v.~T() also must be valid, and, for lvalue v, the expression &v must have the type T* or const T* and must evaluate to the address of v.

(until C++11)

[edit] Notes

Until C++11, classes that overloaded operator& were not CopyConstructible and thus were not usable in the standard library containers. This is a design decision in C++98 (instead of a defect, see LWG issue 390).

Since C++11, the standard library uses std::addressof whenever the address of an object is needed.

Extended content

Being a CopyConstructible class implies std::is_copy_constructible but not vice versa since std::is_copy_constructible will only check for the ability to call the constructor with the correct arguments, and, e.g., not a MoveConstructible requirement.

#include <type_traits>
#include <utility>
 
struct S
{
    S() = default;
    S(S&&) = delete;
    S(const S&) = default;
};
static_assert(std::is_copy_constructible_v<S>);
 
int main()
{
    S s1;
 
    // Class `S` doesn't satisfy MoveConstructible requirement,
    // hence doesn't satisfy CopyConstructible requirement
    [[maybe_unused]] S s2{std::move(s1)}; // ill-formed, use of deleted function
}

[edit] References

Extended content
  • C++23 standard (ISO/IEC 14882:2024):
  • 16.4.4.2 Template argument requirements [utility.arg.requirements]

[edit] See also

checks if a type has a copy constructor
(class template) [edit]
specifies that an object of a type can be copy constructed and move constructed
(concept) [edit]