basic_string_view_operator_cmp

basic_string_view_operator_cmp (basic_string_view 비교 연산자)

이 페이지에서는 std::basic_string_view의 비교 연산자들을 다루고 있어요. 두 문자열 뷰를 비교해서 같은지, 다른지, 혹은 사전식 순서로 어느 쪽이 앞서는지 등을 판단할 수 있어요. C++17에서 처음 도입되었고, C++20부터는 삼방향 비교 연산자(<=>)도 지원돼요.

출처: cppreference

본문

<string_view> 헤더에 정의된 비교 연산자들은 다음과 같아요.

<string_view> 헤더에 정의됨
template < class CharT , class Traits > constexpr bool operator == ( std :: basic_string_view < CharT , Traits > lhs , std :: basic_string_view < CharT , Traits > rhs ) noexcept ; (1) (since C++17) (until 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 ; (since 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) (since C++17) (until 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) (since C++17) (until 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) (since C++17) (until 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) (since C++17) (until 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) (since C++17) (until 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) (since C++20)

두 뷰를 비교해요.

모든 비교는 compare() 멤버 함수를 통해 수행돼요. compare() 자체는 Traits::compare()로 정의되며, 구체적인 규칙은 다음과 같아요.

  • 두 뷰는 lhsrhs의 크기가 같고, 같은 위치에 있는 각 문자가 서로 동등할 때 같다고 판단해요.
  • 순서 비교는 사전식으로 이루어져요. 즉, std::lexicographical_compare와 동등한 함수로 비교를 수행해요.
구현은 basic_string_view<CharT,Traits> 객체 svbasic_string_view<CharT,Traits>로 암시적 변환되는 다른 객체 t와 비교될 수 있도록 충분한 추가 constexprnoexcept 오버로드를 제공해요. 이때 의미는 svbasic_string_view<CharT,Traits>(t)를 비교하는 것과 동일해요. (until C++20)
삼방향 비교 연산자의 반환 타입(/*comp-cat*/)은 해당 한정 식별자(Traits::comparison_category)가 타입을 나타내면 그 타입이고, 그렇지 않으면 std::weak_ordering이에요. 만약 /*comp-cat*/이 비교 범주 타입이 아니라면 프로그램은 ill-formed예요. <, <=, >, >=, != 연산자는 각각 operator<=>operator==로부터 합성돼요. (since C++20)

매개변수

lhs, rhs - 비교할 뷰

반환값

각 연산자의 의미에 따라 bool 또는 비교 범주(comparison category) 값을 반환해요.

복잡도

뷰의 크기에 선형적이에요.

참고 사항

충분한 추가 오버로드는 한 매개변수 타입에서 비연역 문맥(non-deduced context)을 통해 구현될 수 있어요. (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 중복된 추가 오버로드가 여전히 요구되었음 오버로드 집합 축소됨

더 알아보기 (Learn more)

cppreference