Working Draft, Standard for Programming Language C++ (N4713, 2017 year) - page 19

 

  Главная      Manuals     Working Draft, Standard for Programming Language C++ (N4713, 2017 year)

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     17      18      19      20     ..

 

 

 

Working Draft, Standard for Programming Language C++ (N4713, 2017 year) - page 19

 

 

6
Throws: Any exception thrown by the selected constructor of VT.
7
Remarks: If an exception is thrown during the call to VT’s constructor, *this does not contain a value,
and any previously contained value has been destroyed. This function shall not participate in overload
resolution unless is_copy_constructible_v<VT> is true and is_constructible_v<VT, Args...> is
true.
template<class T, class U, class... Args>
decay_t<T>& emplace(initializer_list<U> il, Args&&... args);
8
Let VT be decay_t<T>.
9
Requires: VT shall satisfy the CopyConstructible requirements.
10
Effects: Calls reset(). Then initializes the contained value as if direct-non-list-initializing an object of
type VT with the arguments il, std::forward<Args>(args)
11
Postconditions: *this contains a value.
12
Returns: A reference to the new contained value.
13
Throws: Any exception thrown by the selected constructor of VT.
14
Remarks: If an exception is thrown during the call to VT’s constructor, *this does not contain a
value, and any previously contained value has been destroyed. The function shall not participate
in overload resolution unless is_copy_constructible_v<VT> is true and is_constructible_v<VT,
initializer_list<U>&, Args...> is true.
void reset() noexcept;
15
Effects: If has_value() is true, destroys the contained value.
16
Postconditions: has_value() is false.
void swap(any& rhs) noexcept;
17
Effects: Exchanges the states of *this and rhs.
23.8.3.4
Observers
[any.observers]
bool has_value() const noexcept;
1
Returns: true if *this contains an object, otherwise false.
const type_info& type() const noexcept;
2
Returns: typeid(T) if *this has a contained value of type T, otherwise typeid(void).
3
[Note: Useful for querying against types known either at compile time or only at runtime.
— end
note ]
23.8.4
Non-member functions
[any.nonmembers]
void swap(any& x, any& y) noexcept;
1
Effects: As if by x.swap(y).
template<class T, class... Args>
any make_any(Args&& ...args);
2
Effects: Equivalent to: return any(in_place_type<T>, std::forward<Args>(args)...);
template<class T, class U, class... Args>
any make_any(initializer_list<U> il, Args&& ...args);
3
Effects: Equivalent to: return any(in_place_type<T>, il, std::forward<Args>(args)...);
template<class T>
T any_cast(const any& operand);
template<class T>
T any_cast(any& operand);
template<class T>
T any_cast(any&& operand);
4
Let U be the type remove_cvref_t<T>.
§ 23.8.4
532
5
Requires: For the first overload, is_constructible_v<T, const U&> is true. For the second overload,
is_constructible_v<T, U&> is true. For the third overload, is_constructible_v<T, U> is true.
Otherwise the program is ill-formed.
6
Returns: For the first and second overload, static_cast<T>(*any_cast<U>(&operand)).
For the
third overload, static_cast<T>(std::move(*any_cast<U>(&operand))).
7
Throws: bad_any_cast if operand.type() != typeid(remove_reference_t<T>).
8
[ Example:
any x(5);
// x holds int
assert(any_cast<int>(x) == 5);
// cast to value
any_cast<int&>(x) = 10;
// cast to reference
assert(any_cast<int>(x) == 10);
x = "Meow";
// x holds const char*
assert(strcmp(any_cast<const char*>(x), "Meow") == 0);
any_cast<const char*&>(x) = "Harry";
assert(strcmp(any_cast<const char*>(x), "Harry") == 0);
x = string("Meow");
// x holds string
string s, s2("Jane");
s = move(any_cast<string&>(x));
// move from any
assert(s == "Meow");
any_cast<string&>(x) = move(s2);
// move to any
assert(any_cast<const string&>(x) == "Jane");
string cat("Meow");
const any y(cat);
// const y holds string
assert(any_cast<const string&>(y) == cat);
any_cast<string&>(y);
// error; cannot
// any_cast away const
— end example ]
template<class T>
const T* any_cast(const any* operand) noexcept;
template<class T>
T* any_cast(any* operand) noexcept;
9
Returns: If operand != nullptr && operand->type() == typeid(T), a pointer to the object con-
tained by operand; otherwise, nullptr.
10
[ Example:
bool is_string(const any& operand) {
return any_cast<string>(&operand) != nullptr;
}
— end example ]
23.9
Bitsets
[bitset]
23.9.1
Header <bitset> synopsis
[bitset.syn]
#include <string>
#include <iosfwd>
// for istream (30.7.1), ostream (30.7.2), see 30.3.1
namespace std {
template<size_t N> class bitset;
// 23.9.4, bitset operators
template<size_t N>
bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
template<size_t N>
bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
template<size_t N>
bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
§ 23.9.1
533
template<class charT, class traits, size_t N>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
template<class charT, class traits, size_t N>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
}
1
The header <bitset> defines a class template and several related functions for representing and manipulating
fixed-size sequences of bits.
23.9.2
Class template bitset
[template.bitset]
namespace std {
template<size_t N> class bitset {
public:
// bit reference
class reference {
friend class bitset;
reference() noexcept;
public:
~reference() noexcept;
reference& operator=(bool x) noexcept;
// for b[i] = x;
reference& operator=(const reference&) noexcept;
// for b[i] = b[j];
bool operator~() const noexcept;
// flips the bit
operator bool() const noexcept;
// for x = b[i];
reference& flip() noexcept;
// for b[i].flip();
};
// 23.9.2.1, constructors
constexpr bitset() noexcept;
constexpr bitset(unsigned long long val) noexcept;
template<class charT, class traits, class Allocator>
explicit bitset(
const basic_string<charT, traits, Allocator>& str,
typename basic_string<charT, traits, Allocator>::size_type pos = 0,
typename basic_string<charT, traits, Allocator>::size_type n
= basic_string<charT, traits, Allocator>::npos,
charT zero = charT(’0’),
charT one = charT(’1’));
template<class charT>
explicit bitset(
const charT* str,
typename basic_string<charT>::size_type n = basic_string<charT>::npos,
charT zero = charT(’0’),
charT one = charT(’1’));
// 23.9.2.2, bitset operations
bitset<N>& operator&=(const bitset<N>& rhs) noexcept;
bitset<N>& operator|=(const bitset<N>& rhs) noexcept;
bitset<N>& operator^=(const bitset<N>& rhs) noexcept;
bitset<N>& operator<<=(size_t pos) noexcept;
bitset<N>& operator>>=(size_t pos) noexcept;
bitset<N>& set() noexcept;
bitset<N>& set(size_t pos, bool val = true);
bitset<N>& reset() noexcept;
bitset<N>& reset(size_t pos);
bitset<N> operator~() const noexcept;
bitset<N>& flip() noexcept;
bitset<N>& flip(size_t pos);
// element access
constexpr bool operator[](size_t pos) const;
// for b[i];
reference operator[](size_t pos);
// for b[i];
§
23.9.2
534
unsigned long to_ulong() const;
unsigned long long to_ullong() const;
template<class charT = char,
class traits = char_traits<charT>,
class Allocator = allocator<charT>>
basic_string<charT, traits, Allocator>
to_string(charT zero = charT(’0’), charT one = charT(’1’)) const;
size_t count() const noexcept;
constexpr size_t size() const noexcept;
bool operator==(const bitset<N>& rhs) const noexcept;
bool operator!=(const bitset<N>& rhs) const noexcept;
bool test(size_t pos) const;
bool all() const noexcept;
bool any() const noexcept;
bool none() const noexcept;
bitset<N> operator<<(size_t pos) const noexcept;
bitset<N> operator>>(size_t pos) const noexcept;
};
// 23.9.3, hash support
template<class T> struct hash;
template<size_t N> struct hash<bitset<N>>;
}
1
The class template bitset<N> describes an object that can store a sequence consisting of a fixed number of
bits, N.
2
Each bit represents either the value zero (reset) or one (set). To toggle a bit is to change the value zero to
one, or the value one to zero. Each bit has a non-negative position pos. When converting between an object
of class bitset<N> and a value of some integral type, bit position pos corresponds to the bit value 1 << pos.
The integral value corresponding to two or more bits is the sum of their bit values.
3
The functions described in this subclause can report three kinds of errors, each associated with a distinct
exception:
(3.1)
an invalid-argument error is associated with exceptions of type invalid_argument (22.2.4);
(3.2)
an out-of-range error is associated with exceptions of type out_of_range (22.2.6);
(3.3)
an overflow error is associated with exceptions of type overflow_error (22.2.9).
23.9.2.1
bitset constructors
[bitset.cons]
constexpr bitset() noexcept;
1
Effects: Constructs an object of class bitset<N>, initializing all bits to zero.
constexpr bitset(unsigned long long val) noexcept;
2
Effects: Constructs an object of class bitset<N>, initializing the first M bit positions to the corresponding
bit values in val. M is the smaller of N and the number of bits in the value representation (6.7) of
unsigned long long. If M < N, the remaining bit positions are initialized to zero.
template<class charT, class traits, class Allocator>
explicit bitset(
const basic_string<charT, traits, Allocator>& str,
typename basic_string<charT, traits, Allocator>::size_type pos = 0,
typename basic_string<charT, traits, Allocator>::size_type n
= basic_string<charT, traits, Allocator>::npos,
charT zero = charT(’0’),
charT one = charT(’1’));
3
Throws: out_of_range if pos > str.size() or invalid_argument if an invalid character is found
(see below).
4
Effects: Determines the effective length rlen of the initializing string as the smaller of n and str.size()
- pos.
§ 23.9.2.1
535
The function then throws invalid_argument if any of the rlen characters in str beginning at position
pos is other than zero or one. The function uses traits::eq() to compare the character values.
Otherwise, the function constructs an object of class bitset<N>, initializing the first M bit positions to
values determined from the corresponding characters in the string str. M is the smaller of N and rlen.
5
An element of the constructed object has value zero if the corresponding character in str, beginning
at position pos, is zero. Otherwise, the element has the value one. Character position pos + M - 1
corresponds to bit position zero. Subsequent decreasing character positions correspond to increasing
bit positions.
6
If M
< N, remaining bit positions are initialized to zero.
template<class charT>
explicit bitset(
const charT* str,
typename basic_string<charT>::size_type n = basic_string<charT>::npos,
charT zero = charT(’0’),
charT one = charT(’1’));
7
Effects: Constructs an object of class bitset<N> as if by:
bitset(n == basic_string<charT>::npos
? basic_string<charT>(str)
: basic_string<charT>(str, n),
0, n, zero, one)
23.9.2.2
bitset members
[bitset.members]
bitset<N>& operator&=(const bitset<N>& rhs) noexcept;
1
Effects: Clears each bit in *this for which the corresponding bit in rhs is clear, and leaves all other
bits unchanged.
2
Returns: *this.
bitset<N>& operator|=(const bitset<N>& rhs) noexcept;
3
Effects: Sets each bit in *this for which the corresponding bit in rhs is set, and leaves all other bits
unchanged.
4
Returns: *this.
bitset<N>& operator^=(const bitset<N>& rhs) noexcept;
5
Effects: Toggles each bit in *this for which the corresponding bit in rhs is set, and leaves all other
bits unchanged.
6
Returns: *this.
bitset<N>& operator<<=(size_t pos) noexcept;
7
Effects: Replaces each bit at position I in *this with a value determined as follows:
(7.1)
If I
< pos, the new value is zero;
(7.2)
If I
>= pos, the new value is the previous value of the bit at position I - pos.
8
Returns: *this.
bitset<N>& operator>>=(size_t pos) noexcept;
9
Effects: Replaces each bit at position I in *this with a value determined as follows:
(9.1)
If pos >= N - I, the new value is zero;
(9.2)
If pos < N - I, the new value is the previous value of the bit at position I + pos.
10
Returns: *this.
bitset<N>& set() noexcept;
11
Effects: Sets all bits in *this.
12
Returns: *this.
§ 23.9.2.2
536
bitset<N>& set(size_t pos, bool val = true);
13
Throws: out_of_range if pos does not correspond to a valid bit position.
14
Effects: Stores a new value in the bit at position pos in *this. If val is true, the stored value is one,
otherwise it is zero.
15
Returns: *this.
bitset<N>& reset() noexcept;
16
Effects: Resets all bits in *this.
17
Returns: *this.
bitset<N>& reset(size_t pos);
18
Throws: out_of_range if pos does not correspond to a valid bit position.
19
Effects: Resets the bit at position pos in *this.
20
Returns: *this.
bitset<N> operator~() const noexcept;
21
Effects: Constructs an object x of class bitset<N> and initializes it with *this.
22
Returns: x.flip().
bitset<N>& flip() noexcept;
23
Effects: Toggles all bits in *this.
24
Returns: *this.
bitset<N>& flip(size_t pos);
25
Throws: out_of_range if pos does not correspond to a valid bit position.
26
Effects: Toggles the bit at position pos in *this.
27
Returns: *this.
unsigned long to_ulong() const;
28
Throws: overflow_error if the integral value x corresponding to the bits in *this cannot be represented
as type unsigned long.
29
Returns: x.
unsigned long long to_ullong() const;
30
Throws: overflow_error if the integral value x corresponding to the bits in *this cannot be represented
as type unsigned long long.
31
Returns: x.
template<class charT = char,
class traits = char_traits<charT>,
class Allocator = allocator<charT>>
basic_string<charT, traits, Allocator>
to_string(charT zero = charT(’0’), charT one = charT(’1’)) const;
32
Effects: Constructs a string object of the appropriate type and initializes it to a string of length
N characters. Each character is determined by the value of its corresponding bit position in *this.
Character position N - 1 corresponds to bit position zero. Subsequent decreasing character positions
correspond to increasing bit positions. Bit value zero becomes the character zero, bit value one becomes
the character one.
33
Returns: The created object.
size_t count() const noexcept;
34
Returns: A count of the number of bits set in *this.
§ 23.9.2.2
537
constexpr size_t size() const noexcept;
35
Returns: N.
bool operator==(const bitset<N>& rhs) const noexcept;
36
Returns: true if the value of each bit in *this equals the value of the corresponding bit in rhs.
bool operator!=(const bitset<N>& rhs) const noexcept;
37
Returns: true if !(*this == rhs).
bool test(size_t pos) const;
38
Throws: out_of_range if pos does not correspond to a valid bit position.
39
Returns: true if the bit at position pos in *this has the value one.
bool all() const noexcept;
40
Returns: count() == size().
bool any() const noexcept;
41
Returns: count() != 0.
bool none() const noexcept;
42
Returns: count() == 0.
bitset<N> operator<<(size_t pos) const noexcept;
43
Returns: bitset<N>(*this) <<= pos.
bitset<N> operator>>(size_t pos) const noexcept;
44
Returns: bitset<N>(*this) >>= pos.
constexpr bool operator[](size_t pos) const;
45
Requires: pos shall be valid.
46
Returns: true if the bit at position pos in *this has the value one, otherwise false.
47
Throws: Nothing.
bitset<N>::reference operator[](size_t pos);
48
Requires: pos shall be valid.
49
Returns: An object of type bitset<N>::reference such that (*this)[pos]
== this->test(pos),
and such that (*this)[pos] = val is equivalent to this->set(pos, val).
50
Throws: Nothing.
51
Remarks: For the purpose of determining the presence of a data race (6.8.2), any access or update
through the resulting reference potentially accesses or modifies, respectively, the entire underlying
bitset.
23.9.3
bitset hash support
[bitset.hash]
template<size_t N> struct hash<bitset<N>>;
1
The specialization is enabled (23.14.15).
23.9.4
bitset operators
[bitset.operators]
bitset<N> operator&(const bitset<N>& lhs, const bitset<N>& rhs) noexcept;
1
Returns: bitset<N>(lhs) &= rhs.
bitset<N> operator|(const bitset<N>& lhs, const bitset<N>& rhs) noexcept;
2
Returns: bitset<N>(lhs) |= rhs.
bitset<N> operator^(const bitset<N>& lhs, const bitset<N>& rhs) noexcept;
3
Returns: bitset<N>(lhs) ^= rhs.
§ 23.9.4
538
template<class charT, class traits, size_t N>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
4
A formatted input function (30.7.4.2).
5
Effects: Extracts up to N characters from is. Stores these characters in a temporary object str of type
basic_string<charT, traits>, then evaluates the expression x = bitset<N>(str). Characters are
extracted and stored until any of the following occurs:
(5.1)
N characters have been extracted and stored;
(5.2)
end-of-file occurs on the input sequence;
(5.3)
the next input character is neither is.widen(’0’) nor is.widen(’1’) (in which case the input
character is not extracted).
6
If no characters are stored in str, calls is.setstate(ios_base::failbit) (which may throw ios_-
base::failure (30.5.5.4)).
7
Returns: is.
template<class charT, class traits, size_t N>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
8
Returns:
os << x.template to_string<charT, traits, allocator<charT>>(
use_facet<ctype<charT>>(os.getloc()).widen(’0’),
use_facet<ctype<charT>>(os.getloc()).widen(’1’))
(see 30.7.5.2).
23.10
Memory
[memory]
23.10.1
In general
[memory.general]
1
This subclause describes the contents of the header <memory> (23.10.2) and some of the contents of the
header <cstdlib> (21.2.2).
23.10.2
Header <memory> synopsis
[memory.syn]
1
The header <memory> defines several types and function templates that describe properties of pointers
and pointer-like types, manage memory for containers and other template types, destroy objects, and
construct multiple objects in uninitialized memory buffers (23.10.3-23.10.11). The header also defines the
templates unique_ptr, shared_ptr, weak_ptr, and various function templates that operate on objects of
these types (23.11).
namespace std {
// 23.10.3, pointer traits
template<class Ptr> struct pointer_traits;
template<class T> struct pointer_traits<T*>;
// 23.10.4, pointer conversion
template<class Ptr>
auto to_address(const Ptr& p) noexcept;
template<class T>
constexpr T* to_address(T* p) noexcept;
// 23.10.5, pointer safety
enum class pointer_safety { relaxed, preferred, strict };
void declare_reachable(void* p);
template<class T>
T* undeclare_reachable(T* p);
void declare_no_pointers(char* p, size_t n);
void undeclare_no_pointers(char* p, size_t n);
pointer_safety get_pointer_safety() noexcept;
// 23.10.6, pointer alignment function
void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
§ 23.10.2
539
// 23.10.7, allocator argument tag
struct allocator_arg_t { explicit allocator_arg_t() = default; };
inline constexpr allocator_arg_t allocator_arg{};
// 23.10.8, uses_allocator
template<class T, class Alloc> struct uses_allocator;
// 23.10.9, allocator traits
template<class Alloc> struct allocator_traits;
// 23.10.10, the default allocator
template<class T> class allocator;
template<class T, class U>
bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
template<class T, class U>
bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
// 23.10.11, specialized algorithms
template<class T>
constexpr T* addressof(T& r) noexcept;
template<class T>
const T* addressof(const T&&) = delete;
template<class ForwardIterator>
void uninitialized_default_construct(ForwardIterator first, ForwardIterator
last);
template<class ExecutionPolicy, class ForwardIterator>
void uninitialized_default_construct(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, ForwardIterator
last);
template<class ForwardIterator, class Size>
ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
template<class ExecutionPolicy, class ForwardIterator, class Size>
ForwardIterator uninitialized_default_construct_n(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, Size n);
template<class ForwardIterator>
void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
template<class ExecutionPolicy, class ForwardIterator>
void uninitialized_value_construct(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class Size>
ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
template<class ExecutionPolicy, class ForwardIterator, class Size>
ForwardIterator uninitialized_value_construct_n(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, Size n);
template<class InputIterator, class ForwardIterator>
ForwardIterator uninitialized_copy(InputIterator first, InputIterator last,
ForwardIterator result);
template<class ExecutionPolicy, class InputIterator, class ForwardIterator>
ForwardIterator uninitialized_copy(ExecutionPolicy&& exec, // see 28.4.5
InputIterator first, InputIterator last,
ForwardIterator result);
template<class InputIterator, class Size, class ForwardIterator>
ForwardIterator uninitialized_copy_n(InputIterator first, Size n,
ForwardIterator result);
template<class ExecutionPolicy, class InputIterator, class Size, class ForwardIterator>
ForwardIterator uninitialized_copy_n(ExecutionPolicy&& exec, // see 28.4.5
InputIterator first, Size n,
ForwardIterator result);
template<class InputIterator, class ForwardIterator>
ForwardIterator uninitialized_move(InputIterator first, InputIterator last,
ForwardIterator result);
template<class ExecutionPolicy, class InputIterator, class ForwardIterator>
ForwardIterator uninitialized_move(ExecutionPolicy&& exec, // see 28.4.5
InputIterator first, InputIterator last,
ForwardIterator result);
§
23.10.2
540
template<class InputIterator, class Size, class ForwardIterator>
pair<InputIterator, ForwardIterator> uninitialized_move_n(InputIterator first, Size n,
ForwardIterator result);
template<class ExecutionPolicy, class InputIterator, class Size, class ForwardIterator>
pair<InputIterator, ForwardIterator> uninitialized_move_n(ExecutionPolicy&& exec, // see
28.4.5
InputIterator first, Size
n,
ForwardIterator result);
template<class ForwardIterator, class T>
void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
template<class ExecutionPolicy, class ForwardIterator, class T>
void uninitialized_fill(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, ForwardIterator last, const T& x);
template<class ForwardIterator, class Size, class T>
ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n, const
T& x);
template<class ExecutionPolicy, class ForwardIterator, class Size, class T>
ForwardIterator uninitialized_fill_n(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, Size
n,
const
T& x);
template<class T>
void destroy_at(T* location);
template<class ForwardIterator>
void destroy(ForwardIterator first, ForwardIterator last);
template<class ExecutionPolicy, class ForwardIterator>
void destroy(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class Size>
ForwardIterator destroy_n(ForwardIterator first, Size n);
template<class ExecutionPolicy, class ForwardIterator, class Size>
ForwardIterator destroy_n(ExecutionPolicy&& exec, // see 28.4.5
ForwardIterator first, Size n);
// 23.11.1, class template unique_ptr
template<class T> struct default_delete;
template<class T> struct default_delete<T[]>;
template<class T, class D = default_delete<T>> class unique_ptr;
template<class T, class D> class unique_ptr<T[], D>;
template<class T, class... Args> unique_ptr<T>
make_unique(Args&&... args);
// T is not array
template<class T> unique_ptr<T>
make_unique(size_t n);
// T is U[]
template<class T, class... Args>
unspecified make_unique(Args&&...) = delete;
// T is U[N]
template<class T, class D>
void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
template<class T1, class D1, class T2, class D2>
bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2,
D2>&
y);
template<class T1, class D1, class T2, class D2>
bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2,
D2>&
y);
template<class T1, class D1, class T2, class D2>
bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template<class T1, class D1, class T2, class D2>
bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template<class T1, class D1, class T2, class D2>
bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template<class T1, class D1, class T2, class D2>
bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template<class T, class D>
bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
template<class T, class D>
bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
§
23.10.2
541
template<class T, class D>
bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
template<class T, class D>
bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
template<class T, class D>
bool operator<(const unique_ptr<T, D>& x, nullptr_t);
template<class T, class D>
bool operator<(nullptr_t, const unique_ptr<T, D>& y);
template<class T, class D>
bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
template<class T, class D>
bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
template<class T, class D>
bool operator>(const unique_ptr<T, D>& x, nullptr_t);
template<class T, class D>
bool operator>(nullptr_t, const unique_ptr<T, D>& y);
template<class T, class D>
bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
template<class T, class D>
bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
template<class E, class T, class Y, class D>
basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const
unique_ptr<Y,
D>&
p);
// 23.11.2, class bad_weak_ptr
class bad_weak_ptr;
// 23.11.3, class template shared_ptr
template<class T> class shared_ptr;
// 23.11.3.6, shared_ptr creation
template<class T, class... Args>
shared_ptr<T> make_shared(Args&&... args);
// T is not array
template<class T, class A, class... Args>
shared_ptr<T> allocate_shared(const A& a, Args&&... args);
// T is not array
template<class T>
shared_ptr<T> make_shared(size_t N);
// T is U[]
template<class T, class A>
shared_ptr<T> allocate_shared(const A& a, size_t N);
// T is U[]
template<class T>
shared_ptr<T> make_shared();
// T is U[N]
template<class T, class A>
shared_ptr<T> allocate_shared(const A& a);
// T is U[N]
template<class T>
shared_ptr<T> make_shared(size_t N, const remove_extent_t<T>&
u);
// T is U[]
template<class T, class A>
shared_ptr<T> allocate_shared(const A& a, size_t N,
const remove_extent_t<T>& u);
// T is U[]
template<class T> shared_ptr<T>
make_shared(const remove_extent_t<T>& u);
// T is U[N]
template<class T, class A>
shared_ptr<T> allocate_shared(const A& a, const remove_extent_t<T>&
u);
// T is U[N]
// 23.11.3.7, shared_ptr comparisons
template<class T, class U>
bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
template<class T, class U>
bool operator!=(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
template<class T, class U>
bool operator<(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
§
23.10.2
542
template<class T, class U>
bool operator>(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept;
template<class T, class U>
bool operator<=(const shared_ptr<T>& a, const shared_ptr<U>&
b)
noexcept;
template<class T, class U>
bool operator>=(const shared_ptr<T>& a, const shared_ptr<U>&
b)
noexcept;
template<class T>
bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
template<class T>
bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
template<class T>
bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
template<class T>
bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
template<class T>
bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
template<class T>
bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
template<class T>
bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
template<class T>
bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
template<class T>
bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
template<class T>
bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
template<class T>
bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
template<class T>
bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
// 23.11.3.8, shared_ptr specialized algorithms
template<class T>
void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
// 23.11.3.9, shared_ptr casts
template<class T, class U>
shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept;
template<class T, class U>
shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r) noexcept;
template<class T, class U>
shared_ptr<T> const_pointer_cast(const shared_ptr<U>& r) noexcept;
template<class T, class U>
shared_ptr<T> reinterpret_pointer_cast(const shared_ptr<U>& r) noexcept;
// 23.11.3.10, shared_ptr get_deleter
template<class D, class T>
D* get_deleter(const shared_ptr<T>& p) noexcept;
// 23.11.3.11, shared_ptr I/O
template<class E, class T, class Y>
basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const shared_ptr<Y>&
p);
// 23.11.4, class template weak_ptr
template<class T> class weak_ptr;
// 23.11.4.6, weak_ptr specialized algorithms
template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
// 23.11.5, class template owner_less
template<class T = void> struct owner_less;
§ 23.10.2
543
// 23.11.6, class template enable_shared_from_this
template<class T> class enable_shared_from_this;
// 23.11.7, hash support
template<class T> struct hash;
template<class T, class D> struct hash<unique_ptr<T, D>>;
template<class T> struct hash<shared_ptr<T>>;
// 23.11.8, atomic smart pointers
template<class T> struct atomic<shared_ptr<T>>;
template<class T> struct atomic<weak_ptr<T>>;
// 23.10.8.1, uses_allocator
template<class T, class Alloc>
inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
}
23.10.3
Pointer traits
[pointer.traits]
1
The class template pointer_traits supplies a uniform interface to certain attributes of pointer-like types.
namespace std {
template<class Ptr> struct pointer_traits {
using pointer
= Ptr;
using element_type
= see below ;
using difference_type = see below ;
template<class U> using rebind = see below ;
static pointer pointer_to(see below r);
};
template<class T> struct pointer_traits<T*> {
using pointer
= T*;
using element_type
= T;
using difference_type = ptrdiff_t;
template<class U> using rebind = U*;
static pointer pointer_to(see below r) noexcept;
};
}
23.10.3.1
Pointer traits member types
[pointer.traits.types]
using element_type = see below ;
1
Type: Ptr::element_type if the qualified-id Ptr::element_type is valid and denotes a type (17.9.2);
otherwise, T if Ptr is a class template instantiation of the form SomePointer<T, Args>, where Args is
zero or more type arguments; otherwise, the specialization is ill-formed.
using difference_type = see below ;
2
Type: Ptr::difference_type if the qualified-id Ptr::difference_type is valid and denotes a type
(17.9.2); otherwise, ptrdiff_t.
template<class U> using rebind = see below ;
3
Alias template: Ptr::rebind<U> if the qualified-id Ptr::rebind<U> is valid and denotes a type (17.9.2);
otherwise, SomePointer<U, Args> if Ptr is a class template instantiation of the form SomePointer<T,
Args>, where Args is zero or more type arguments; otherwise, the instantiation of rebind is ill-formed.
23.10.3.2
Pointer traits member functions
[pointer.traits.functions]
static pointer pointer_traits::pointer_to(see below r);
static pointer pointer_traits<T*>::pointer_to(see below r) noexcept;
1
Remarks: If element_type is cv void, the type of r is unspecified; otherwise, it is element_type&.
§ 23.10.3.2
544
2
Returns: The first member function returns a pointer to r obtained by calling Ptr::pointer_to(r)
through which indirection is valid; an instantiation of this function is ill-formed if Ptr does not have a
matching pointer_to static member function. The second member function returns addressof(r).
23.10.3.3
Pointer traits optional members
[pointer.traits.optmem]
1
Specializations of pointer_traits may define the member declared in this subclause to customize the
behavior of the standard library.
static element_type* to_address(pointer p) noexcept;
2
Returns: A pointer of type element_type* that references the same location as the argument p.
3
[ Note: This function should be the inverse of pointer_to. If defined, it customizes the behavior of the
non-member function to_address (23.10.4).
— end note ]
23.10.4
Pointer conversion
[pointer.conversion]
template<class Ptr> auto to_address(const Ptr& p) noexcept;
1
Returns: pointer_traits<Ptr>::to_address(p) if that expression is well-formed (see 23.10.3.3),
otherwise to_address(p.operator->()).
template<class T> constexpr T* to_address(T* p) noexcept;
2
Requires: T is not a function type. Otherwise the program is ill-formed.
3
Returns: p.
23.10.5
Pointer safety
[util.dynamic.safety]
1
A complete object is declared reachable while the number of calls to declare_reachable with an argument
referencing the object exceeds the number of calls to undeclare_reachable with an argument referencing
the object.
void declare_reachable(void* p);
2
Requires: p shall be a safely-derived pointer (6.6.4.4.3) or a null pointer value.
3
Effects: If p is not null, the complete object referenced by p is subsequently declared reachable (6.6.4.4.3).
4
Throws: May throw bad_alloc if the system cannot allocate additional memory that may be required
to track objects declared reachable.
template<class T> T* undeclare_reachable(T* p);
5
Requires: If p is not null, the complete object referenced by p shall have been previously declared
reachable, and shall be live (6.6.3) from the time of the call until the last undeclare_reachable(p)
call on the object.
6
Returns: A safely derived copy of p which shall compare equal to p.
7
Throws: Nothing.
8
[ Note: It is expected that calls to declare_reachable(p) will consume a small amount of memory in
addition to that occupied by the referenced object until the matching call to undeclare_reachable(p)
is encountered. Long running programs should arrange that calls are matched.
— end note ]
void declare_no_pointers(char* p, size_t n);
9
Requires: No bytes in the specified range are currently registered with declare_no_pointers(). If
the specified range is in an allocated object, then it shall be entirely within a single allocated object.
The object shall be live until the corresponding undeclare_no_pointers() call. [ Note: In a garbage-
collecting implementation, the fact that a region in an object is registered with declare_no_pointers()
should not prevent the object from being collected.
— end note ]
10
Effects: The n bytes starting at p no longer contain traceable pointer locations, independent of their
type. Hence indirection through a pointer located there is undefined if the object it points to was
created by global operator new and not previously declared reachable. [Note: This may be used to
inform a garbage collector or leak detector that this region of memory need not be traced.
— end
note ]
11
Throws: Nothing.
§ 23.10.5
545
12
[Note: Under some conditions implementations may need to allocate memory. However, the request
can be ignored if memory allocation fails.
— end note ]
void undeclare_no_pointers(char* p, size_t n);
13
Requires: The same range shall previously have been passed to declare_no_pointers().
14
Effects: Unregisters a range registered with declare_no_pointers() for destruction. It shall be called
before the lifetime of the object ends.
15
Throws: Nothing.
pointer_safety get_pointer_safety() noexcept;
16
Returns: pointer_safety::strict if the implementation has strict pointer safety (6.6.4.4.3). It is
implementation-defined whether get_pointer_safety returns pointer_safety::relaxed or point-
er_safety::preferred if the implementation has relaxed pointer safety.225
23.10.6
Align
[ptr.align]
void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
1
Effects: If it is possible to fit size bytes of storage aligned by alignment into the buffer pointed to by
ptr with length space, the function updates ptr to represent the first possible address of such storage
and decreases space by the number of bytes used for alignment. Otherwise, the function does nothing.
2
Requires:
(2.1)
alignment shall be a power of two
(2.2)
ptr shall represent the address of contiguous storage of at least space bytes
3
Returns: A null pointer if the requested aligned buffer would not fit into the available space, otherwise
the adjusted value of ptr.
4
[Note: The function updates its ptr and space arguments so that it can be called repeatedly with
possibly different alignment and size arguments for the same buffer.
— end note ]
23.10.7
Allocator argument tag
[allocator.tag]
namespace std {
struct allocator_arg_t { explicit allocator_arg_t() = default; };
inline constexpr allocator_arg_t allocator_arg{};
}
1
The allocator_arg_t struct is an empty structure type used as a unique type to disambiguate constructor
and function overloading. Specifically, several types (see tuple 23.5) have constructors with allocator_-
arg_t as the first argument, immediately followed by an argument of a type that satisfies the Allocator
requirements (20.5.3.5).
23.10.8
uses_allocator
[allocator.uses]
23.10.8.1
uses_allocator trait
[allocator.uses.trait]
template<class T, class Alloc> struct uses_allocator;
1
Remarks: Automatically detects whether T has a nested allocator_type that is convertible from Alloc.
Meets the BinaryTypeTrait requirements (23.15.1). The implementation shall provide a definition that
is derived from true_type if the qualified-id T::allocator_type is valid and denotes a type (17.9.2)
and is_convertible_v<Alloc, T::allocator_type> != false, otherwise it shall be derived from
false_type. A program may specialize this template to derive from true_type for a user-defined type
T that does not have a nested allocator_type but nonetheless can be constructed with an allocator
where either:
(1.1)
the first argument of a constructor has type allocator_arg_t and the second argument has type
Alloc or
(1.2)
the last argument of a constructor has type Alloc.
225) pointer_safety::preferred might be returned to indicate that a leak detector is running so that the program can avoid
spurious leak reports.
§ 23.10.8.1
546
23.10.8.2
Uses-allocator construction
[allocator.uses.construction]
1
Uses-allocator construction with allocator Alloc refers to the construction of an object obj of type T, using
constructor arguments v1, v2, ..., vN of types V1, V2, ..., VN, respectively, and an allocator alloc of
type Alloc, according to the following rules:
(1.1)
if uses_allocator_v<T, Alloc> is false and is_constructible_v<T, V1, V2, ..., VN> is true,
then obj is initialized as obj(v1, v2, ..., vN);
(1.2)
otherwise, if uses_allocator_v<T, Alloc> is true and is_constructible_v<T, allocator_arg_t,
Alloc, V1, V2, ..., VN> is true, then obj is initialized as obj(allocator_arg, alloc, v1, v2,
..., vN);
(1.3)
otherwise, if uses_allocator_v<T, Alloc> is true and is_constructible_v<T, V1, V2, ..., VN,
Alloc> is true, then obj is initialized as obj(v1, v2, ..., vN, alloc);
(1.4)
otherwise, the request for uses-allocator construction is ill-formed.
[Note: An error will result if
uses_allocator_v<T, Alloc> is true but the specific constructor does not take an allocator. This
definition prevents a silent failure to pass the allocator to an element.
— end note ]
23.10.9
Allocator traits
[allocator.traits]
1
The class template allocator_traits supplies a uniform interface to all allocator types. An allocator cannot
be a non-class type, however, even if allocator_traits supplies the entire required interface. [Note:
Thus,
it is always possible to create a derived class from an allocator.
— end note ]
namespace std {
template<class Alloc> struct allocator_traits {
using allocator_type
= Alloc;
using value_type
= typename Alloc::value_type;
using pointer
= see below ;
using const_pointer
= see below ;
using void_pointer
= see below ;
using const_void_pointer = see below ;
using difference_type
= see below ;
using size_type
= see below ;
using propagate_on_container_copy_assignment = see below ;
using propagate_on_container_move_assignment = see below ;
using propagate_on_container_swap
= see below ;
using is_always_equal
= see below ;
template<class T> using rebind_alloc = see below ;
template<class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
[[nodiscard]] static pointer allocate(Alloc& a, size_type n);
[[nodiscard]] static pointer allocate(Alloc& a, size_type n, const_void_pointer
hint);
static void deallocate(Alloc& a, pointer p, size_type n);
template<class T, class... Args>
static void construct(Alloc& a, T* p, Args&&... args);
template<class T>
static void destroy(Alloc& a, T* p);
static size_type max_size(const Alloc& a) noexcept;
static Alloc select_on_container_copy_construction(const Alloc& rhs);
};
}
§ 23.10.9
547
23.10.9.1
Allocator traits member types
[allocator.traits.types]
using pointer = see below ;
1
Type: Alloc::pointer if the qualified-id Alloc::pointer is valid and denotes a type (17.9.2); otherwise,
value_type*.
using const_pointer = see below ;
2
Type: Alloc::const_pointer if the qualified-id Alloc::const_pointer is valid and denotes a type
(17.9.2); otherwise, pointer_traits<pointer>::rebind<const value_type>.
using void_pointer = see below ;
3
Type: Alloc::void_pointer if the qualified-id Alloc::void_pointer is valid and denotes a type
(17.9.2); otherwise, pointer_traits<pointer>::rebind<void>.
using const_void_pointer = see below ;
4
Type: Alloc::const_void_pointer if the qualified-id Alloc::const_void_pointer is valid and de-
notes a type (17.9.2); otherwise, pointer_traits<pointer>::rebind<const void>.
using difference_type = see below ;
5
Type: Alloc::difference_type if the qualified-id Alloc::difference_type is valid and denotes a
type (17.9.2); otherwise, pointer_traits<pointer>::difference_type.
using size_type = see below ;
6
Type: Alloc::size_type if the qualified-id Alloc::size_type is valid and denotes a type (17.9.2);
otherwise, make_unsigned_t<difference_type>.
using propagate_on_container_copy_assignment = see below ;
7
Type: Alloc::propagate_on_container_copy_assignment if the qualified-id Alloc::propagate_-
on_container_copy_assignment is valid and denotes a type (17.9.2); otherwise false_type.
using propagate_on_container_move_assignment = see below ;
8
Type: Alloc::propagate_on_container_move_assignment if the qualified-id Alloc::propagate_-
on_container_move_assignment is valid and denotes a type (17.9.2); otherwise false_type.
using propagate_on_container_swap = see below ;
9
Type: Alloc::propagate_on_container_swap if the qualified-id Alloc::propagate_on_container_-
swap is valid and denotes a type (17.9.2); otherwise false_type.
using is_always_equal = see below ;
10
Type: Alloc::is_always_equal if the qualified-id Alloc::is_always_equal is valid and denotes a
type (17.9.2); otherwise is_empty<Alloc>::type.
template<class T> using rebind_alloc = see below ;
11
Alias template: Alloc::rebind<T>::other if the qualified-id Alloc::rebind<T>::other is valid and
denotes a type (17.9.2); otherwise, Alloc<T, Args> if Alloc is a class template instantiation of the
form Alloc<U, Args>, where Args is zero or more type arguments; otherwise, the instantiation of
rebind_alloc is ill-formed.
23.10.9.2
Allocator traits static member functions
[allocator.traits.members]
[[nodiscard]] static pointer allocate(Alloc& a, size_type n);
1
Returns: a.allocate(n).
[[nodiscard]] static pointer allocate(Alloc& a, size_type n, const_void_pointer hint);
2
Returns: a.allocate(n, hint) if that expression is well-formed; otherwise, a.allocate(n).
static void deallocate(Alloc& a, pointer p, size_type n);
3
Effects: Calls a.deallocate(p, n).
4
Throws: Nothing.
§ 23.10.9.2
548
template<class T, class... Args>
static void construct(Alloc& a, T* p, Args&&... args);
5
Effects: Calls a.construct(p, std::forward<Args>(args)...) if that call is well-formed; otherwise,
invokes ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...).
template<class T>
static void destroy(Alloc& a, T* p);
6
Effects: Calls a.destroy(p) if that call is well-formed; otherwise, invokes p->~T().
static size_type max_size(const Alloc& a) noexcept;
7
Returns: a.max_size() if that expression is well-formed; otherwise, numeric_limits<size_type>::
max()/sizeof(value_type).
static Alloc select_on_container_copy_construction(const Alloc& rhs);
8
Returns: rhs.select_on_container_copy_construction() if that expression is well-formed; other-
wise, rhs.
23.10.10
The default allocator
[default.allocator]
1
All specializations of the default allocator satisfy the allocator completeness requirements (20.5.3.5.1).
namespace std {
template<class T> class allocator {
public:
using value_type
= T;
using propagate_on_container_move_assignment = true_type;
using is_always_equal = true_type;
allocator() noexcept;
allocator(const allocator&) noexcept;
template<class U> allocator(const allocator<U>&) noexcept;
~allocator();
[[nodiscard]] T* allocate(size_t n);
void deallocate(T* p, size_t n);
};
}
23.10.10.1
allocator members
[allocator.members]
1
Except for the destructor, member functions of the default allocator shall not introduce data races (6.8.2)
as a result of concurrent calls to those member functions from different threads. Calls to these functions
that allocate or deallocate a particular unit of storage shall occur in a single total order, and each such
deallocation call shall happen before the next allocation (if any) in this order.
[[nodiscard]] T* allocate(size_t n);
2
Returns: A pointer to the initial element of an array of storage of size n * sizeof(T), aligned
appropriately for objects of type T.
3
Remarks: the storage is obtained by calling ::operator new (21.6.2), but it is unspecified when or
how often this function is called.
4
Throws: bad_alloc if the storage cannot be obtained.
void deallocate(T* p, size_t n);
5
Requires: p shall be a pointer value obtained from allocate(). n shall equal the value passed as the
first argument to the invocation of allocate which returned p.
6
Effects: Deallocates the storage referenced by p .
7
Remarks: Uses ::operator delete (21.6.2), but it is unspecified when this function is called.
§ 23.10.10.1
549
23.10.10.2
allocator globals
[allocator.globals]
template<class T, class U>
bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
1
Returns: true.
template<class T, class U>
bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
2
Returns: false.
23.10.11
Specialized algorithms
[specialized.algorithms]
1
Throughout this subclause, the names of template parameters are used to express type requirements.
(1.1)
If an algorithm’s template parameter is named InputIterator, the template argument shall satisfy
the requirements of an input iterator (27.2.3).
(1.2)
If an algorithm’s template parameter is named ForwardIterator, the template argument shall satisfy
the requirements of a forward iterator (27.2.5), and is required to have the property that no exceptions
are thrown from increment, assignment, comparison, or indirection through valid iterators.
Unless otherwise specified, if an exception is thrown in the following algorithms there are no effects.
23.10.11.1
addressof
[specialized.addressof]
template<class T> constexpr T* addressof(T& r) noexcept;
1
Returns: The actual address of the object or function referenced by r, even in the presence of an
overloaded operator&.
2
Remarks: An expression addressof(E) is a constant subexpression (20.3.6) if E is an lvalue constant
subexpression.
23.10.11.2
uninitialized_default_construct
[uninitialized.construct.default]
template<class ForwardIterator>
void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
1
Effects: Equivalent to:
for (; first != last; ++first)
::new (static_cast<void*>(addressof(*first)))
typename iterator_traits<ForwardIterator>::value_type;
template<class ForwardIterator, class Size>
ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
2
Effects: Equivalent to:
for (; n > 0; (void)++first, --n)
::new (static_cast<void*>(addressof(*first)))
typename iterator_traits<ForwardIterator>::value_type;
return first;
23.10.11.3
uninitialized_value_construct
[uninitialized.construct.value]
template<class ForwardIterator>
void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
1
Effects: Equivalent to:
for (; first != last; ++first)
::new (static_cast<void*>(addressof(*first)))
typename iterator_traits<ForwardIterator>::value_type();
template<class ForwardIterator, class Size>
ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
2
Effects: Equivalent to:
for (; n > 0; (void)++first, --n)
::new (static_cast<void*>(addressof(*first)))
typename iterator_traits<ForwardIterator>::value_type();
§ 23.10.11.3
550
return first;
23.10.11.4
uninitialized_copy
[uninitialized.copy]
template<class InputIterator, class ForwardIterator>
ForwardIterator uninitialized_copy(InputIterator first, InputIterator last,
ForwardIterator result);
1
Effects: As if by:
for (; first != last; ++result, (void) ++first)
::new (static_cast<void*>(addressof(*result)))
typename iterator_traits<ForwardIterator>::value_type(*first);
2
Returns: result.
template<class InputIterator, class Size, class ForwardIterator>
ForwardIterator uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
3
Effects: As if by:
for ( ; n > 0; ++result, (void) ++first, --n) {
::new (static_cast<void*>(addressof(*result)))
typename iterator_traits<ForwardIterator>::value_type(*first);
}
4
Returns: result.
23.10.11.5
uninitialized_move
[uninitialized.move]
template<class InputIterator, class ForwardIterator>
ForwardIterator uninitialized_move(InputIterator first, InputIterator last,
ForwardIterator result);
1
Effects: Equivalent to:
for (; first != last; (void)++result, ++first)
::new (static_cast<void*>(addressof(*result)))
typename iterator_traits<ForwardIterator>::value_type(std::move(*first));
return result;
2
Remarks: If an exception is thrown, some objects in the range [first, last) are left in a valid but
unspecified state.
template<class InputIterator, class Size, class ForwardIterator>
pair<InputIterator, ForwardIterator>
uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
3
Effects: Equivalent to:
for (; n > 0; ++result, (void) ++first, --n)
::new (static_cast<void*>(addressof(*result)))
typename iterator_traits<ForwardIterator>::value_type(std::move(*first));
return {first,result};
4
Remarks: If an exception is thrown, some objects in the range [first, std::next(first,n)) are left
in a valid but unspecified state.
23.10.11.6
uninitialized_fill
[uninitialized.fill]
template<class ForwardIterator, class T>
void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
1
Effects: As if by:
for (; first != last; ++first)
::new (static_cast<void*>(addressof(*first)))
typename iterator_traits<ForwardIterator>::value_type(x);
template<class ForwardIterator, class Size, class T>
ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
2
Effects: As if by:
§ 23.10.11.6
551
for (; n--; ++first)
::new (static_cast<void*>(addressof(*first)))
typename iterator_traits<ForwardIterator>::value_type(x);
return first;
23.10.11.7
destroy
[specialized.destroy]
template<class T>
void destroy_at(T* location);
1
Effects: Equivalent to:
location->~T();
template<class ForwardIterator>
void destroy(ForwardIterator first, ForwardIterator last);
2
Effects: Equivalent to:
for (; first!=last; ++first)
destroy_at(addressof(*first));
template<class ForwardIterator, class Size>
ForwardIterator destroy_n(ForwardIterator first, Size n);
3
Effects: Equivalent to:
for (; n > 0; (void)++first, --n)
destroy_at(addressof(*first));
return first;
23.10.12
C library memory allocation
[c.malloc]
1
[ Note: The header <cstdlib> (21.2.2) declares the functions described in this subclause.
— end note ]
void* aligned_alloc(size_t alignment, size_t size);
void* calloc(size_t nmemb, size_t size);
void* malloc(size_t size);
void* realloc(void* ptr, size_t size);
2
Effects: These functions have the semantics specified in the C standard library.
3
Remarks: These functions do not attempt to allocate storage by calling ::operator
new() (21.6).
4
Storage allocated directly with these functions is implicitly declared reachable (see 6.6.4.4.3) on
allocation, ceases to be declared reachable on deallocation, and need not cease to be declared reachable
as the result of an undeclare_reachable() call. [Note: This allows existing C libraries to remain
unaffected by restrictions on pointers that are not safely derived, at the expense of providing far fewer
garbage collection and leak detection options for malloc()-allocated objects. It also allows malloc()
to be implemented with a separate allocation arena, bypassing the normal declare_reachable()
implementation. The above functions should never intentionally be used as a replacement for declare_-
reachable(), and newly written code is strongly encouraged to treat memory allocated with these
functions as though it were allocated with operator new.
— end note ]
void free(void* ptr);
5
Effects: This function has the semantics specified in the C standard library.
6
Remarks: This function does not attempt to deallocate storage by calling ::operator delete().
See also: ISO C 7.22.3
23.11
Smart pointers
[smartptr]
23.11.1
Class template unique_ptr
[unique.ptr]
1
A unique pointer is an object that owns another object and manages that other object through a pointer.
More precisely, a unique pointer is an object u that stores a pointer to a second object p and will dispose of
p when u is itself destroyed (e.g., when leaving block scope (9.7)). In this context, u is said to own p.
2
The mechanism by which u disposes of p is known as p’s associated deleter, a function object whose correct
invocation results in p’s appropriate disposition (typically its deletion).
§ 23.11.1
552
3
Let the notation u.p denote the pointer stored by u, and let u.d denote the associated deleter. Upon request,
u can reset (replace) u.p and u.d with another pointer and deleter, but properly disposes of its owned object
via the associated deleter before such replacement is considered completed.
4
Additionally, u can, upon request, transfer ownership to another unique pointer u2. Upon completion of such
a transfer, the following postconditions hold:
(4.1)
u2.p is equal to the pre-transfer u.p,
(4.2)
u.p is equal to nullptr, and
(4.3)
if the pre-transfer u.d maintained state, such state has been transferred to u2.d.
As in the case of a reset, u2 properly disposes of its pre-transfer owned object via the pre-transfer associated
deleter before the ownership transfer is considered complete. [ Note: A deleter’s state need never be copied,
only moved or swapped as ownership is transferred.
— end note ]
5
Each object of a type U instantiated from the unique_ptr template specified in this subclause has the strict
ownership semantics, specified above, of a unique pointer. In partial satisfaction of these semantics, each
such U is MoveConstructible and MoveAssignable, but is not CopyConstructible nor CopyAssignable.
The template parameter T of unique_ptr may be an incomplete type.
6
[ Note: The uses of unique_ptr include providing exception safety for dynamically allocated memory, passing
ownership of dynamically allocated memory to a function, and returning dynamically allocated memory from
a function.
— end note ]
23.11.1.1
Default deleters
[unique.ptr.dltr]
23.11.1.1.1
In general
[unique.ptr.dltr.general]
1
The class template default_delete serves as the default deleter (destruction policy) for the class template
unique_ptr.
2
The template parameter T of default_delete may be an incomplete type.
23.11.1.1.2
default_delete
[unique.ptr.dltr.dflt]
namespace std {
template<class T> struct default_delete {
constexpr default_delete() noexcept = default;
template<class U> default_delete(const default_delete<U>&) noexcept;
void operator()(T*) const;
};
}
template<class U> default_delete(const default_delete<U>& other) noexcept;
1
Effects: Constructs a default_delete object from another default_delete<U> object.
2
Remarks: This constructor shall not participate in overload resolution unless U* is implicitly convertible
to T*.
void operator()(T* ptr) const;
3
Effects: Calls delete on ptr.
4
Remarks: If T is an incomplete type, the program is ill-formed.
23.11.1.1.3
default_delete<T[]>
[unique.ptr.dltr.dflt1]
namespace std {
template<class T> struct default_delete<T[]> {
constexpr default_delete() noexcept = default;
template<class U> default_delete(const default_delete<U[]>&) noexcept;
template<class U> void operator()(U* ptr) const;
};
}
template<class U> default_delete(const default_delete<U[]>& other) noexcept;
1
Effects: Constructs a default_delete object from another default_delete<U[]> object.
2
Remarks: This constructor shall not participate in overload resolution unless U(*)[] is convertible to
T(*)[].
§ 23.11.1.1.3
553
template<class U> void operator()(U* ptr) const;
3
Effects: Calls delete[] on ptr.
4
Remarks: If U is an incomplete type, the program is ill-formed. This function shall not participate in
overload resolution unless U(*)[] is convertible to T(*)[].
23.11.1.2
unique_ptr for single objects
[unique.ptr.single]
namespace std {
template<class T, class D = default_delete<T>> class unique_ptr
{
public:
using pointer
= see below ;
using element_type = T;
using deleter_type = D;
// 23.11.1.2.1, constructors
constexpr unique_ptr() noexcept;
explicit unique_ptr(pointer p) noexcept;
unique_ptr(pointer p, see below d1) noexcept;
unique_ptr(pointer p, see below d2) noexcept;
unique_ptr(unique_ptr&& u) noexcept;
constexpr unique_ptr(nullptr_t) noexcept;
template<class U, class E>
unique_ptr(unique_ptr<U, E>&& u) noexcept;
// 23.11.1.2.2, destructor
~unique_ptr();
// 23.11.1.2.3, assignment
unique_ptr& operator=(unique_ptr&& u) noexcept;
template<class U, class E>
unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
unique_ptr& operator=(nullptr_t) noexcept;
// 23.11.1.2.4, observers
add_lvalue_reference_t<T> operator*() const;
pointer operator->() const noexcept;
pointer get() const noexcept;
deleter_type& get_deleter() noexcept;
const deleter_type& get_deleter() const noexcept;
explicit operator bool() const noexcept;
// 23.11.1.2.5, modifiers
pointer release() noexcept;
void reset(pointer p = pointer()) noexcept;
void swap(unique_ptr& u) noexcept;
// disable copy from lvalue
unique_ptr(const unique_ptr&) = delete;
unique_ptr& operator=(const unique_ptr&) = delete;
};
}
1
The default type for the template parameter D is default_delete. A client-supplied template argument D
shall be a function object type (23.14), lvalue reference to function, or lvalue reference to function object type
for which, given a value d of type D and a value ptr of type unique_ptr<T, D>::pointer, the expression
d(ptr) is valid and has the effect of disposing of the pointer as appropriate for that deleter.
2
If the deleter’s type D is not a reference type, D shall satisfy the requirements of Destructible (Table 27).
3
If the qualified-id remove_reference_t<D>::pointer is valid and denotes a type (17.9.2), then unique_-
ptr<T, D>::pointer shall be a synonym for remove_reference_t<D>::pointer. Otherwise unique_ptr<T,
D>::pointer shall be a synonym for element_type*. The type unique_ptr<T, D>::pointer shall satisfy
the requirements of NullablePointer (20.5.3.3).
§ 23.11.1.2
554
4
[Example: Given an allocator type X (20.5.3.5) and letting A be a synonym for allocator_traits<X>, the
types A::pointer, A::const_pointer, A::void_pointer, and A::const_void_pointer may be used as
unique_ptr<T, D>::pointer. — end example ]
23.11.1.2.1
unique_ptr constructors
[unique.ptr.single.ctor]
constexpr unique_ptr() noexcept;
constexpr unique_ptr(nullptr_t) noexcept;
1
Requires: D shall satisfy the requirements of DefaultConstructible (Table 22), and that construction
shall not throw an exception.
2
Effects: Constructs a unique_ptr object that owns nothing, value-initializing the stored pointer and
the stored deleter.
3
Postconditions: get() == nullptr. get_deleter() returns a reference to the stored deleter.
4
Remarks: If is_pointer_v<deleter_type> is true or is_default_constructible_v<deleter_-
type> is false, this constructor shall not participate in overload resolution.
explicit unique_ptr(pointer p) noexcept;
5
Requires: D shall satisfy the requirements of DefaultConstructible (Table 22), and that construction
shall not throw an exception.
6
Effects: Constructs a unique_ptr which owns p, initializing the stored pointer with p and value-
initializing the stored deleter.
7
Postconditions: get() == p. get_deleter() returns a reference to the stored deleter.
8
Remarks: If is_pointer_v<deleter_type> is true or is_default_constructible_v<deleter_-
type> is false, this constructor shall not participate in overload resolution. If class template argument
deduction (16.3.1.8) would select the function template corresponding to this constructor, then the
program is ill-formed.
unique_ptr(pointer p, see below d1) noexcept;
unique_ptr(pointer p, see below d2) noexcept;
9
The signature of these constructors depends upon whether D is a reference type. If D is a non-reference
type A, then the signatures are:
unique_ptr(pointer p, const A& d) noexcept;
unique_ptr(pointer p, A&& d) noexcept;
10
If D is an lvalue reference type A&, then the signatures are:
unique_ptr(pointer p, A& d) noexcept;
unique_ptr(pointer p, A&& d) = delete;
11
If D is an lvalue reference type const A&, then the signatures are:
unique_ptr(pointer p, const A& d) noexcept;
unique_ptr(pointer p, const A&& d) = delete;
12
Requires: For the first constructor, if D is not a reference type, D shall satisfy the requirements of
CopyConstructible and such construction shall not exit via an exception. For the second constructor, if
D is not a reference type, D shall satisfy the requirements of MoveConstructible and such construction
shall not exit via an exception.
13
Effects: Constructs a unique_ptr object which owns p, initializing the stored pointer with p and
initializing the deleter from std::forward<decltype(d)>(d).
14
Remarks: These constructors shall not participate in overload resolution unless is_constructible_v<D,
decltype(d)> is true.
15
Postconditions: get() == p. get_deleter() returns a reference to the stored deleter. If D is a
reference type then get_deleter() returns a reference to the lvalue d.
16
Remarks: If class template argument deduction (16.3.1.8) would select a function template corresponding
to either of these constructors, then the program is ill-formed.
17
[ Example:
D d;
§
23.11.1.2.1
555
unique_ptr<int, D> p1(new int, D());
// D must be MoveConstructible
unique_ptr<int, D> p2(new int, d);
// D must be CopyConstructible
unique_ptr<int, D&> p3(new int, d);
// p3 holds a reference to d
unique_ptr<int, const D&> p4(new int, D()); // error: rvalue deleter object combined
// with reference deleter type
— end example ]
unique_ptr(unique_ptr&& u) noexcept;
18
Requires: If D is not a reference type, D shall satisfy the requirements of MoveConstructible (Table 23).
Construction of the deleter from an rvalue of type D shall not throw an exception.
19
Effects: Constructs a unique_ptr by transferring ownership from u to *this. If D is a reference type,
this deleter is copy constructed from u’s deleter; otherwise, this deleter is move constructed from u’s
deleter. [ Note: The deleter constructor can be implemented with std::forward<D>.
— end note ]
20
Postconditions: get() yields the value u.get() yielded before the construction. get_deleter() returns
a reference to the stored deleter that was constructed from u.get_deleter(). If D is a reference type
then get_deleter() and u.get_deleter() both reference the same lvalue deleter.
template<class U, class E> unique_ptr(unique_ptr<U, E>&& u) noexcept;
21
Requires: If E is not a reference type, construction of the deleter from an rvalue of type E shall be
well-formed and shall not throw an exception. Otherwise, E is a reference type and construction of the
deleter from an lvalue of type E shall be well-formed and shall not throw an exception.
22
Remarks: This constructor shall not participate in overload resolution unless:
(22.1)
unique_ptr<U, E>::pointer is implicitly convertible to pointer,
(22.2)
U is not an array type, and
(22.3)
either D is a reference type and E is the same type as D, or D is not a reference type and E is
implicitly convertible to D.
23
Effects: Constructs a unique_ptr by transferring ownership from u to *this. If E is a reference type,
this deleter is copy constructed from u’s deleter; otherwise, this deleter is move constructed from u’s
deleter. [ Note: The deleter constructor can be implemented with std::forward<E>.
— end note ]
24
Postconditions: get() yields the value u.get() yielded before the construction. get_deleter() returns
a reference to the stored deleter that was constructed from u.get_deleter().
23.11.1.2.2
unique_ptr destructor
[unique.ptr.single.dtor]
~unique_ptr();
1
Requires: The expression get_deleter()(get()) shall be well-formed, shall have well-defined behavior,
and shall not throw exceptions. [ Note: The use of default_delete requires T to be a complete type.
— end note ]
2
Effects: If get() == nullptr there are no effects. Otherwise get_deleter()(get()).
23.11.1.2.3
unique_ptr assignment
[unique.ptr.single.asgn]
unique_ptr& operator=(unique_ptr&& u) noexcept;
1
Requires: If D is not a reference type, D shall satisfy the requirements of MoveAssignable (Table 25)
and assignment of the deleter from an rvalue of type D shall not throw an exception. Otherwise,
D is a reference type; remove_reference_t<D> shall satisfy the CopyAssignable requirements and
assignment of the deleter from an lvalue of type D shall not throw an exception.
2
Effects: Transfers ownership from u to *this as if by calling reset(u.release()) followed by get_-
deleter() = std::forward<D>(u.get_deleter()).
3
Returns: *this.
template<class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
4
Requires: If E is not a reference type, assignment of the deleter from an rvalue of type E shall be
well-formed and shall not throw an exception. Otherwise, E is a reference type and assignment of the
deleter from an lvalue of type E shall be well-formed and shall not throw an exception.
§ 23.11.1.2.3
556
5
Remarks: This operator shall not participate in overload resolution unless:
(5.1)
unique_ptr<U, E>::pointer is implicitly convertible to pointer, and
(5.2)
U is not an array type, and
(5.3)
is_assignable_v<D&, E&&> is true.
6
Effects: Transfers ownership from u to *this as if by calling reset(u.release()) followed by get_-
deleter() = std::forward<E>(u.get_deleter()).
7
Returns: *this.
unique_ptr& operator=(nullptr_t) noexcept;
8
Effects: As if by reset().
9
Postconditions: get() == nullptr.
10
Returns: *this.
23.11.1.2.4
unique_ptr observers
[unique.ptr.single.observers]
add_lvalue_reference_t<T> operator*() const;
1
Requires: get() != nullptr.
2
Returns: *get().
pointer operator->() const noexcept;
3
Requires: get() != nullptr.
4
Returns: get().
5
[ Note: The use of this function typically requires that T be a complete type.
— end note ]
pointer get() const noexcept;
6
Returns: The stored pointer.
deleter_type& get_deleter() noexcept;
const deleter_type& get_deleter() const noexcept;
7
Returns: A reference to the stored deleter.
explicit operator bool() const noexcept;
8
Returns: get() != nullptr.
23.11.1.2.5
unique_ptr modifiers
[unique.ptr.single.modifiers]
pointer release() noexcept;
1
Postconditions: get() == nullptr.
2
Returns: The value get() had at the start of the call to release.
void reset(pointer p = pointer()) noexcept;
3
Requires: The expression get_deleter()(get()) shall be well-formed, shall have well-defined behavior,
and shall not throw exceptions.
4
Effects: Assigns p to the stored pointer, and then if and only if the old value of the stored pointer,
old_p, was not equal to nullptr, calls get_deleter()(old_p). [ Note: The order of these operations
is significant because the call to get_deleter() may destroy *this.
— end note ]
5
Postconditions: get() == p. [Note: The postcondition does not hold if the call to get_deleter()
destroys *this since this->get() is no longer a valid expression.
— end note ]
void swap(unique_ptr& u) noexcept;
6
Requires: get_deleter() shall be swappable (20.5.3.2) and shall not throw an exception under swap.
7
Effects: Invokes swap on the stored pointers and on the stored deleters of *this and u.
§ 23.11.1.2.5
557
23.11.1.3
unique_ptr for array objects with a runtime length
[unique.ptr.runtime]
namespace std {
template<class T, class D> class unique_ptr<T[], D> {
public:
using pointer
= see below ;
using element_type = T;
using deleter_type = D;
// 23.11.1.3.1, constructors
constexpr unique_ptr() noexcept;
template<class U> explicit unique_ptr(U p) noexcept;
template<class U> unique_ptr(U p, see below d) noexcept;
template<class U> unique_ptr(U p, see below d) noexcept;
unique_ptr(unique_ptr&& u) noexcept;
template<class U, class E>
unique_ptr(unique_ptr<U, E>&& u) noexcept;
constexpr unique_ptr(nullptr_t) noexcept;
// destructor
~unique_ptr();
// assignment
unique_ptr& operator=(unique_ptr&& u) noexcept;
template<class U, class E>
unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
unique_ptr& operator=(nullptr_t) noexcept;
// 23.11.1.3.3, observers
T& operator[](size_t i) const;
pointer get() const noexcept;
deleter_type& get_deleter() noexcept;
const deleter_type& get_deleter() const noexcept;
explicit operator bool() const noexcept;
// 23.11.1.3.4, modifiers
pointer release() noexcept;
template<class U> void reset(U p) noexcept;
void reset(nullptr_t = nullptr) noexcept;
void swap(unique_ptr& u) noexcept;
// disable copy from lvalue
unique_ptr(const unique_ptr&) = delete;
unique_ptr& operator=(const unique_ptr&) = delete;
};
}
1
A specialization for array types is provided with a slightly altered interface.
(1.1)
Conversions between different types of unique_ptr<T[], D> that would be disallowed for the cor-
responding pointer-to-array types, and conversions to or from the non-array forms of unique_ptr,
produce an ill-formed program.
(1.2)
Pointers to types derived from T are rejected by the constructors, and by reset.
(1.3)
The observers operator* and operator-> are not provided.
(1.4)
The indexing observer operator[] is provided.
(1.5)
The default deleter will call delete[].
2
Descriptions are provided below only for members that differ from the primary template.
3
The template argument T shall be a complete type.
§ 23.11.1.3
558
23.11.1.3.1
unique_ptr constructors
[unique.ptr.runtime.ctor]
template<class U> explicit unique_ptr(U p) noexcept;
1
This constructor behaves the same as the constructor in the primary template that takes a single
parameter of type pointer except that it additionally shall not participate in overload resolution unless
(1.1)
U is the same type as pointer, or
(1.2)
pointer is the same type as element_type*, U is a pointer type V*, and V(*)[] is convertible to
element_type(*)[].
template<class U> unique_ptr(U p, see below d) noexcept;
template<class U> unique_ptr(U p, see below d) noexcept;
2
These constructors behave the same as the constructors in the primary template that take a parameter
of type pointer and a second parameter except that they shall not participate in overload resolution
unless either
(2.1)
U is the same type as pointer,
(2.2)
U is nullptr_t, or
(2.3)
pointer is the same type as element_type*, U is a pointer type V*, and V(*)[] is convertible to
element_type(*)[].
template<class U, class E> unique_ptr(unique_ptr<U, E>&& u) noexcept;
3
This constructor behaves the same as in the primary template, except that it shall not participate in
overload resolution unless all of the following conditions hold, where UP is unique_ptr<U, E>:
(3.1)
U is an array type, and
(3.2)
pointer is the same type as element_type*, and
(3.3)
UP::pointer is the same type as UP::element_type*, and
(3.4)
UP::element_type(*)[] is convertible to element_type(*)[], and
(3.5)
either D is a reference type and E is the same type as D, or D is not a reference type and E is
implicitly convertible to D.
[ Note: This replaces the overload-resolution specification of the primary template — end note ]
23.11.1.3.2
unique_ptr assignment
[unique.ptr.runtime.asgn]
template<class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u)noexcept;
1
This operator behaves the same as in the primary template, except that it shall not participate in
overload resolution unless all of the following conditions hold, where UP is unique_ptr<U, E>:
(1.1)
U is an array type, and
(1.2)
pointer is the same type as element_type*, and
(1.3)
UP::pointer is the same type as UP::element_type*, and
(1.4)
UP::element_type(*)[] is convertible to element_type(*)[], and
(1.5)
is_assignable_v<D&, E&&> is true.
[ Note: This replaces the overload-resolution specification of the primary template — end note ]
23.11.1.3.3
unique_ptr observers
[unique.ptr.runtime.observers]
T& operator[](size_t i) const;
1
Requires: i < the number of elements in the array to which the stored pointer points.
2
Returns: get()[i].
23.11.1.3.4
unique_ptr modifiers
[unique.ptr.runtime.modifiers]
void reset(nullptr_t p = nullptr) noexcept;
1
Effects: Equivalent to reset(pointer()).
§ 23.11.1.3.4
559
template<class U> void reset(U p) noexcept;
2
This function behaves the same as the reset member of the primary template, except that it shall not
participate in overload resolution unless either
(2.1)
U is the same type as pointer, or
(2.2)
pointer is the same type as element_type*, U is a pointer type V*, and V(*)[] is convertible to
element_type(*)[].
23.11.1.4
unique_ptr creation
[unique.ptr.create]
template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
1
Remarks: This function shall not participate in overload resolution unless T is not an array.
2
Returns: unique_ptr<T>(new T(std::forward<Args>(args)...)).
template<class T> unique_ptr<T> make_unique(size_t n);
3
Remarks: This function shall not participate in overload resolution unless T is an array of unknown
bound.
4
Returns: unique_ptr<T>(new remove_extent_t<T>[n]()).
template<class T, class... Args> unspecified make_unique(Args&&...) = delete;
5
Remarks: This function shall not participate in overload resolution unless T is an array of known bound.
23.11.1.5
unique_ptr specialized algorithms
[unique.ptr.special]
template<class T, class D> void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
1
Remarks: This function shall not participate in overload resolution unless is_swappable_v<D> is true.
2
Effects: Calls x.swap(y).
template<class T1, class D1, class T2, class D2>
bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
3
Returns: x.get() == y.get().
template<class T1, class D1, class T2, class D2>
bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
4
Returns: x.get() != y.get().
template<class T1, class D1, class T2, class D2>
bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
5
Requires: Let CT denote
common_type_t<typename unique_ptr<T1, D1>::pointer,
typename unique_ptr<T2, D2>::pointer>
Then the specialization less<CT > shall be a function object type (23.14) that induces a strict weak
ordering (28.7) on the pointer values.
6
Returns: less<CT >()(x.get(), y.get()).
7
Remarks: If unique_ptr<T1, D1>::pointer is not implicitly convertible to CT or unique_ptr<T2,
D2>::pointer is not implicitly convertible to CT , the program is ill-formed.
template<class T1, class D1, class T2, class D2>
bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
8
Returns: !(y < x).
template<class T1, class D1, class T2, class D2>
bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
9
Returns: y < x.
template<class T1, class D1, class T2, class D2>
bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
10
Returns: !(x < y).
§ 23.11.1.5
560
template<class T, class D>
bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
template<class T, class D>
bool operator==(nullptr_t, const unique_ptr<T, D>& x) noexcept;
11
Returns: !x.
template<class T, class D>
bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
template<class T, class D>
bool operator!=(nullptr_t, const unique_ptr<T, D>& x) noexcept;
12
Returns: (bool)x.
template<class T, class D>
bool operator<(const unique_ptr<T, D>& x, nullptr_t);
template<class T, class D>
bool operator<(nullptr_t, const unique_ptr<T, D>& x);
13
Requires: The specialization less<unique_ptr<T, D>::pointer> shall be a function object type (23.14)
that induces a strict weak ordering (28.7) on the pointer values.
14
Returns: The first function template returns
less<unique_ptr<T, D>::pointer>()(x.get(), nullptr)
The second function template returns
less<unique_ptr<T, D>::pointer>()(nullptr, x.get())
template<class T, class D>
bool operator>(const unique_ptr<T, D>& x, nullptr_t);
template<class T, class D>
bool operator>(nullptr_t, const unique_ptr<T, D>& x);
15
Returns: The first function template returns nullptr < x. The second function template returns x <
nullptr.
template<class T, class D>
bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
template<class T, class D>
bool operator<=(nullptr_t, const unique_ptr<T, D>& x);
16
Returns: The first function template returns !(nullptr < x). The second function template returns
!(x < nullptr).
template<class T, class D>
bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
template<class T, class D>
bool operator>=(nullptr_t, const unique_ptr<T, D>& x);
17
Returns: The first function template returns !(x < nullptr). The second function template returns
!(nullptr < x).
23.11.1.6
unique_ptr I/O
[unique.ptr.io]
template<class E, class T, class Y, class D>
basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const unique_ptr<Y, D>& p);
1
Effects: Equivalent to: os << p.get();
2
Returns: os.
3
Remarks: This function shall not participate in overload resolution unless os << p.get() is a valid
expression.
23.11.2
Class bad_weak_ptr
[util.smartptr.weak.bad]
namespace std {
class bad_weak_ptr : public exception {
public:
bad_weak_ptr() noexcept;
§ 23.11.2
561

 

 

 

 

 

 

 

Content      ..     17      18      19      20     ..