Namespaces
Variants

operator==,!=,<,<=,>,>=,<=> (std::basic_string_view)

From cppreference.net
헤더에 정의됨 <string_view>
(1)
template < class CharT, class Traits >

constexpr bool operator == ( std:: basic_string_view < CharT,Traits > lhs,

std:: basic_string_view < CharT,Traits > rhs ) noexcept ;
(C++17부터)
(C++20 이전까지)
template < class CharT, class Traits >

constexpr bool operator == (
std:: basic_string_view < CharT,Traits > lhs,

std:: type_identity_t < std:: basic_string_view < CharT,Traits >> rhs ) noexcept ;
(C++20부터)
template < class CharT, class Traits >

constexpr bool operator ! = ( std:: basic_string_view < CharT,Traits > lhs,

std:: basic_string_view < CharT,Traits > rhs ) noexcept ;
(2) (C++17부터)
(C++20 이전까지)
template < class CharT, class Traits >

constexpr bool operator < ( std:: basic_string_view < CharT,Traits > lhs,

std:: basic_string_view < CharT,Traits > rhs ) noexcept ;
(3) (C++17부터)
(C++20까지)
template < class CharT, class Traits >

constexpr bool operator <= ( std:: basic_string_view < CharT,Traits > lhs,

std:: basic_string_view < CharT,Traits > rhs ) noexcept ;
(4) (C++17 이후)
(C++20 이전)
template < class CharT, class Traits >

constexpr bool operator > ( std:: basic_string_view < CharT,Traits > lhs,

std:: basic_string_view < CharT,Traits > rhs ) noexcept ;
(5) (C++17부터)
(C++20까지)
template < class CharT, class Traits >

constexpr bool operator >= ( std:: basic_string_view < CharT,Traits > lhs,

std:: basic_string_view < CharT,Traits > rhs ) noexcept ;
(6) (C++17부터)
(C++20까지)
template < class CharT, class Traits >

constexpr /*comp-cat*/ operator <=> (
std:: basic_string_view < CharT,Traits > lhs,

std:: type_identity_t < std:: basic_string_view < CharT,Traits >> rhs ) noexcept ;
(7) (C++20 이후)

두 뷰를 비교합니다.

모든 비교는 compare() 멤버 함수를 통해 수행됩니다(이 함수 자체는 Traits::compare() 를 기반으로 정의됨):

  • 두 뷰는 lhs rhs 의 크기가 모두 같고, lhs 의 각 문자가 동일한 위치에서 rhs 의 해당 문자와 동등할 경우 동일합니다.

구현은 이러한 함수들의 충분한 추가 constexpr noexcept 오버로드를 제공하여, basic_string_view<CharT,Traits> 객체 sv basic_string_view<CharT,Traits> 로의 암시적 변환이 가능한 다른 객체 t 와 비교될 수 있도록 하며, 이때 의미는 sv basic_string_view<CharT,Traits>(t) 를 비교하는 것과 동일합니다.

(C++20 이전)

3-way 비교 연산자들( /*comp-cat*/ )의 반환 타입은 해당 한정자 ID( Traits :: comparison_category )가 타입을 나타내는 경우 그 타입이며, 그렇지 않으면 std::weak_ordering 입니다. /*comp-cat*/ 이 비교 범주 타입이 아닌 경우 프로그램은 ill-formed입니다.

< , <= , > , >= , 및 != 연산자들은 각각 synthesized 되어 operator <=> operator == 로부터 생성됩니다.

(C++20 이후)

목차

매개변수

lhs, rhs - 비교할 뷰

반환값

1-6) true 해당 비교가 성립하는 경우, false 그렇지 않은 경우.
7) static_cast < /*comp-cat*/ > ( lhs. compare ( rhs ) <=> 0 ) .

복잡도

뷰의 크기에 선형적으로 비례합니다.

참고 사항

충분한 추가 오버로드는 하나의 매개변수 타입에서 비추론 문맥을 통해 구현될 수 있습니다.

(until C++20)

std::string_view , std::wstring_view , std::u8string_view , std::u16string_view std::u32string_view 의 삼중 비교 결과 타입은 std::strong_ordering 입니다.

std::type_identity_t 은 비추론 문맥에 사용되며, 이는 문자열 뷰 타입으로 암시적으로 변환 가능한 인자들이 문자열 뷰와 비교 가능하도록 만듭니다.

(since C++20)

예제

#include <string_view>
int main()
{
    using namespace std::literals;
    static_assert(""sv == ""sv);
    static_assert(""sv == "", "Selects an additional overload until C++20.");
    static_assert("" == ""sv, "Selects an additional overload until C++20."
                              "Uses a rewritten candidate since C++20.");
    static_assert(!(""sv != ""sv), "Uses the rewritten candidate since C++20.");
    static_assert(!(""sv != ""), "Selects an additional overload until C++20;"
                                 "Uses a rewritten candidate since C++20.");
    static_assert(!("" != ""sv), "Selects an additional overload until C++20."
                                 "Uses a rewritten candidate since C++20.");
}

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3432 C++20 operator<=> 의 반환 타입이 비교 범주 타입일 필요가 없었음 필수로 지정됨
LWG 3950 C++20 여전히 불필요한 추가 오버로드가 필요했음 오버로드 세트 축소됨