[string.compare]

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

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

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

#### 27.4.3.8 String operations [[string.ops]](string.ops#string.compare)

#### 27.4.3.8.4 basic_string​::​compare [string.compare]

[ð](#lib:compare,basic_string)

`template<class T>
  constexpr int compare(const T& t) const noexcept(see below);
`

[1](#1)

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

*Constraints*: 

- [(1.1)](#1.1)

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

- [(1.2)](#1.2)

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

[2](#2)

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

*Effects*: Equivalent to: return basic_string_view<charT, traits>(*this).compare(t);

[3](#3)

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

*Remarks*: The exception specification is equivalent tois_nothrow_convertible_v<const T&, basic_string_view<charT, traits>>[.](#3.sentence-1)

[ð](#lib:compare,basic_string_)

`template<class T>
  constexpr int compare(size_type pos1, size_type n1, const T& t) const;
`

[4](#4)

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

*Constraints*: 

- [(4.1)](#4.1)

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

- [(4.2)](#4.2)

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

[5](#5)

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

*Effects*: Equivalent to:return basic_string_view<charT, traits>(*this).substr(pos1, n1).compare(t);

[ð](#lib:compare,basic_string__)

`template<class T>
  constexpr int compare(size_type pos1, size_type n1, const T& t,
                        size_type pos2, size_type n2 = npos) const;
`

[6](#6)

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

*Constraints*: 

- [(6.1)](#6.1)

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

- [(6.2)](#6.2)

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

[7](#7)

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

*Effects*: Equivalent to:basic_string_view<charT, traits> s = *this, sv = t;return s.substr(pos1, n1).compare(sv.substr(pos2, n2));

[ð](#lib:compare,basic_string___)

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

[8](#8)

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

*Effects*: Equivalent to:return compare(basic_string_view<charT, traits>(str));

[ð](#lib:compare,basic_string____)

`constexpr int compare(size_type pos1, size_type n1, const basic_string& str) const;
`

[9](#9)

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

*Effects*: Equivalent to:return compare(pos1, n1, basic_string_view<charT, traits>(str));

[ð](#lib:compare,basic_string_____)

`constexpr int compare(size_type pos1, size_type n1, const basic_string& str,
                      size_type pos2, size_type n2 = npos) const;
`

[10](#10)

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

*Effects*: Equivalent to:return compare(pos1, n1, basic_string_view<charT, traits>(str), pos2, n2);

[ð](#lib:compare,basic_string______)

`constexpr int compare(const charT* s) const;
`

[11](#11)

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

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

[ð](#lib:compare,basic_string_______)

`constexpr int compare(size_type pos, size_type n1, const charT* s) const;
`

[12](#12)

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

*Effects*: Equivalent to: return compare(pos, n1, basic_string_view<charT, traits>(s));

[ð](#lib:compare,basic_string________)

`constexpr int compare(size_type pos, size_type n1, const charT* s, size_type n2) const;
`

[13](#13)

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

*Effects*: Equivalent to: return compare(pos, n1, basic_string_view<charT, traits>(s, n2));
