Files
2025-10-25 03:02:53 +03:00

13 KiB
Raw Permalink Blame History

[string.cons]

27 Strings library [strings]

27.4 String classes [string.classes]

27.4.3 Class template basic_string [basic.string]

27.4.3.3 Constructors and assignment operators [string.cons]

🔗

constexpr explicit basic_string(const Allocator& a) noexcept;

1

#

Postconditions: size() is equal to 0.

🔗

constexpr basic_string(const basic_string& str); constexpr basic_string(basic_string&& str) noexcept;

2

#

Effects: Constructs an object whose value is that of str prior to this call.

3

#

Remarks: In the second form, str is left in a valid but unspecified state.

🔗

constexpr basic_string(const basic_string& str, size_type pos, const Allocator& a = Allocator()); constexpr basic_string(const basic_string& str, size_type pos, size_type n, const Allocator& a = Allocator()); constexpr basic_string(basic_string&& str, size_type pos, const Allocator& a = Allocator()); constexpr basic_string(basic_string&& str, size_type pos, size_type n, const Allocator& a = Allocator());

4

#

Let

s be the value of str prior to this call and

rlen be pos + min(n, s.size() - pos) for the overloads with parameter n, ands.size() otherwise.

5

#

Effects: Constructs an object whose initial value is the range [s.data() + pos, s.data() + rlen).

6

#

Throws: out_of_range if pos > s.size().

7

#

Remarks: For the overloads with a basic_string&& parameter,str is left in a valid but unspecified state.

8

#

Recommended practice: For the overloads with a basic_string&& parameter, implementations should avoid allocation if s.get_allocator() == a is true.

🔗

template<class T> constexpr basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator());

9

#

Constraints: is_convertible_v<const T&, basic_string_view<charT, traits>> is true.

10

#

Effects: Creates a variable, sv, as if by basic_string_view<charT, traits> sv = t; and then behaves the same as:basic_string(sv.substr(pos, n), a);

🔗

template<class T> constexpr explicit basic_string(const T& t, const Allocator& a = Allocator());

11

#

Constraints:

is_convertible_v<const T&, basic_string_view<charT, traits>> istrue and

is_convertible_v<const T&, const charT*> isfalse.

12

#

Effects: Creates a variable, sv, as if bybasic_string_view<charT, traits> sv = t; and then behaves the same as basic_string(sv.data(), sv.size(), a).

🔗

constexpr basic_string(const charT* s, size_type n, const Allocator& a = Allocator());

13

#

Preconditions: [s, s + n) is a valid range.

14

#

Effects: Constructs an object whose initial value is the range [s, s + n).

15

#

Postconditions: size() is equal to n, andtraits::compare(data(), s, n) is equal to 0.

🔗

constexpr basic_string(const charT* s, const Allocator& a = Allocator());

16

#

Constraints: Allocator is a type that qualifies as an allocator ([container.reqmts]).

[Note 1:

This affects class template argument deduction.

— end note]

17

#

Effects: Equivalent to: basic_string(s, traits::length(s), a).

🔗

constexpr basic_string(size_type n, charT c, const Allocator& a = Allocator());

18

#

Constraints: Allocator is a type that qualifies as an allocator ([container.reqmts]).

[Note 2:

This affects class template argument deduction.

— end note]

19

#

Effects: Constructs an object whose value consists of n copies of c.

🔗

template<class InputIterator> constexpr basic_string(InputIterator begin, InputIterator end, const Allocator& a = Allocator());

20

#

Constraints: InputIterator is a type that qualifies as an input iterator ([container.reqmts]).

21

#

Effects: Constructs a string from the values in the range [begin, end), as specified in [sequence.reqmts].

🔗

template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<charT> R> constexpr basic_string(from_range_t, R&& rg, const Allocator& = Allocator());

22

#

Effects: Constructs a string from the values in the range rg, as specified in [sequence.reqmts].

🔗

constexpr basic_string(initializer_list<charT> il, const Allocator& a = Allocator());

23

#

Effects: Equivalent to basic_string(il.begin(), il.end(), a).

🔗

constexpr basic_string(const basic_string& str, const Allocator& alloc); constexpr basic_string(basic_string&& str, const Allocator& alloc);

24

#

Effects: Constructs an object whose value is that of str prior to this call.

The stored allocator is constructed from alloc.

In the second form, str is left in a valid but unspecified state.

25

#

Throws: The second form throws nothing if alloc == str.get_allocator().

🔗

template<class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>> basic_string(InputIterator, InputIterator, Allocator = Allocator()) -> basic_string<typename iterator_traits<InputIterator>::value_type, char_traits<typename iterator_traits<InputIterator>::value_type>, Allocator>;

26

#

Constraints: InputIterator is a type that qualifies as an input iterator, and Allocator is a type that qualifies as an allocator ([container.reqmts]).

🔗

`template<class charT, class traits, class Allocator = allocator> explicit basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator()) -> basic_string<charT, traits, Allocator>;

template<class charT, class traits, class Allocator = allocator> basic_string(basic_string_view<charT, traits>, typename see below::size_type, typename see below::size_type, const Allocator& = Allocator()) -> basic_string<charT, traits, Allocator>; `

27

#

Constraints: Allocator is a type that qualifies as an allocator ([container.reqmts]).

🔗

constexpr basic_string& operator=(const basic_string& str);

28

#

Effects: If *this and str are the same object, has no effect.

Otherwise, replaces the value of *this with a copy of str.

29

#

Returns: *this.

🔗

constexpr basic_string& operator=(basic_string&& str) noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value);

30

#

Effects: Move assigns as a sequence container ([sequence.reqmts]), except that iterators, pointers and references may be invalidated.

31

#

Returns: *this.

🔗

template<class T> constexpr basic_string& operator=(const T& t);

32

#

Constraints:

is_convertible_v<const T&, basic_string_view<charT, traits>> is true and

is_convertible_v<const T&, const charT*> is false.

33

#

Effects: Equivalent to:basic_string_view<charT, traits> sv = t;return assign(sv);

🔗

constexpr basic_string& operator=(const charT* s);

34

#

Effects: Equivalent to:return *this = basic_string_view<charT, traits>(s);

🔗

constexpr basic_string& operator=(charT c);

35

#

Effects: Equivalent to:return *this = basic_string_view<charT, traits>(addressof(c), 1);

🔗

constexpr basic_string& operator=(initializer_list<charT> il);

36

#

Effects: Equivalent to:return *this = basic_string_view<charT, traits>(il.begin(), il.size());