[string.cons]

# 27 Strings library [[strings]](./#strings)

## 27.4 String classes [[string.classes]](string.classes#string.cons)

### 27.4.3 Class template basic_string [[basic.string]](basic.string#string.cons)

#### 27.4.3.3 Constructors and assignment operators [string.cons]

[ð](#lib:basic_string,constructor)

`constexpr explicit basic_string(const Allocator& a) noexcept;
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2431)

*Postconditions*: size() is equal to 0[.](#1.sentence-1)

[ð](#lib:basic_string,constructor_)

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

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2443)

*Effects*: Constructs an object whose
value is that of str prior to this call[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2448)

*Remarks*: In the second form, str is left in a valid but unspecified state[.](#3.sentence-1)

[ð](#lib:basic_string,constructor__)

`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](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2466)

Let

- [(4.1)](#4.1)

s be the value of str prior to this call and

- [(4.2)](#4.2)

rlen be pos + min(n, s.size() - pos) for the overloads with parameter n, ands.size() otherwise[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2477)

*Effects*: Constructs an object
whose initial value is the range [s.data() + pos, s.data() + rlen)[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2482)

*Throws*: out_of_range if pos > s.size()[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2486)

*Remarks*: For the overloads with a basic_string&& parameter,str is left in a valid but unspecified state[.](#7.sentence-1)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2491)

*Recommended practice*: For the overloads with a basic_string&& parameter,
implementations should avoid allocation
if s.get_allocator() == a is true[.](#8.sentence-1)

[ð](#lib:basic_string,constructor___)

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

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2505)

*Constraints*: is_convertible_v<const T&, basic_string_view<charT, traits>> is true[.](#9.sentence-1)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2510)

*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);

[ð](#lib:basic_string,constructor____)

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

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2527)

*Constraints*: 

- [(11.1)](#11.1)

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

- [(11.2)](#11.2)

is_convertible_v<const T&, const charT*> isfalse[.](#11.sentence-1)

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2538)

*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)[.](#12.sentence-1)

[ð](#lib:basic_string,constructor_____)

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

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2551)

*Preconditions*: [s, s + n) is a valid range[.](#13.sentence-1)

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2555)

*Effects*: Constructs an object whose initial value is the range [s, s + n)[.](#14.sentence-1)

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2559)

*Postconditions*: size() is equal to n, andtraits​::​compare(data(), s, n) is equal to 0[.](#15.sentence-1)

[ð](#lib:basic_string,constructor______)

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

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2571)

*Constraints*: Allocator is a type
that qualifies as an allocator ([[container.reqmts]](container.reqmts "23.2.2.2 Container requirements"))[.](#16.sentence-1)

[*Note [1](#note-1)*:

This affects class template argument deduction[.](#16.sentence-2)

â *end note*]

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2579)

*Effects*: Equivalent to: basic_string(s, traits​::​length(s), a)[.](#17.sentence-1)

[ð](#lib:basic_string,constructor_______)

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

[18](#18)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2590)

*Constraints*: Allocator is a type
that qualifies as an allocator ([[container.reqmts]](container.reqmts "23.2.2.2 Container requirements"))[.](#18.sentence-1)

[*Note [2](#note-2)*:

This affects class template argument deduction[.](#18.sentence-2)

â *end note*]

[19](#19)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2598)

*Effects*: Constructs an object whose value consists of n copies of c[.](#19.sentence-1)

[ð](#lib:basic_string,constructor________)

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

[20](#20)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2610)

*Constraints*: InputIterator is a type that qualifies as an input
iterator ([[container.reqmts]](container.reqmts "23.2.2.2 Container requirements"))[.](#20.sentence-1)

[21](#21)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2615)

*Effects*: Constructs a string from the values in the range [begin, end),
as specified in [[sequence.reqmts]](sequence.reqmts "23.2.4 Sequence containers")[.](#21.sentence-1)

[ð](#lib:basic_string,constructor_________)

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

[22](#22)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2628)

*Effects*: Constructs a string from the values in the range rg,
as specified in [[sequence.reqmts]](sequence.reqmts "23.2.4 Sequence containers")[.](#22.sentence-1)

[ð](#lib:basic_string,constructor__________)

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

[23](#23)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2640)

*Effects*: Equivalent to basic_string(il.begin(), il.end(), a)[.](#23.sentence-1)

[ð](#lib:basic_string,constructor___________)

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

[24](#24)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2652)

*Effects*: Constructs an object whose value is
that of str prior to this call[.](#24.sentence-1)

The stored allocator is constructed from alloc[.](#24.sentence-2)

In the second form, str is left in a valid but unspecified state[.](#24.sentence-3)

[25](#25)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2659)

*Throws*: The second form throws nothing if alloc == str.get_allocator()[.](#25.sentence-1)

[ð](#itemdecl:13)

`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](#26)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2674)

*Constraints*: InputIterator is a type that qualifies as an input iterator,
and Allocator is a type that qualifies as an allocator ([[container.reqmts]](container.reqmts "23.2.2.2 Container requirements"))[.](#26.sentence-1)

[ð](#itemdecl:14)

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

template<class charT,
         class traits,
         class Allocator = allocator<charT>>
  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](#27)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2697)

*Constraints*: Allocator is a type that qualifies as
an allocator ([[container.reqmts]](container.reqmts "23.2.2.2 Container requirements"))[.](#27.sentence-1)

[ð](#lib:operator=,basic_string)

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

[28](#28)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2709)

*Effects*: If *this and str are the same object, has no effect[.](#28.sentence-1)

Otherwise, replaces the value of *this with a copy of str[.](#28.sentence-2)

[29](#29)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2714)

*Returns*: *this[.](#29.sentence-1)

[ð](#lib:operator=,basic_string_)

`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](#30)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2727)

*Effects*: Move assigns as a sequence container ([[sequence.reqmts]](sequence.reqmts "23.2.4 Sequence containers")),
except that iterators, pointers and references may be invalidated[.](#30.sentence-1)

[31](#31)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2732)

*Returns*: *this[.](#31.sentence-1)

[ð](#lib:operator=,basic_string__)

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

[32](#32)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2744)

*Constraints*: 

- [(32.1)](#32.1)

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

- [(32.2)](#32.2)

is_convertible_v<const T&, const charT*> is false[.](#32.sentence-1)

[33](#33)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2753)

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

[ð](#lib:operator=,basic_string___)

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

[34](#34)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2768)

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

[ð](#lib:operator=,basic_string____)

`constexpr basic_string& operator=(charT c);
`

[35](#35)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2780)

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

[ð](#lib:operator=,basic_string_____)

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

[36](#36)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L2794)

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