Allow comparison with string_view and converted to

Using identity of trick, the template argument is only deduced on
the first (or the second) argument and therefore allows implicit
conversion for one of them.
This commit is contained in:
Baptiste Wicht 2018-04-05 12:12:30 +02:00
parent 5388d9ee12
commit 28b6076147

View File

@ -180,6 +180,10 @@ static_assert(sizeof(string_view) == 16, "The size of a string_view must always
// TODO Switch to C++14 and use constexpr for these functions
// Note: The identity_of_t trick is simply to ensure that template argument
// deduction is only done on one of the two arguments. This is allowing implicit
// conversion (string and string_view for instance)
// non-member comparison functions
template <typename CharT>
@ -187,31 +191,91 @@ bool operator==(basic_string_view<CharT> x, basic_string_view<CharT> y) noexcept
return x.compare(y) == 0;
}
template <typename CharT>
bool operator==(basic_string_view<CharT> x, std::identity_of_t<basic_string_view<CharT>> y) noexcept {
return x.compare(y) == 0;
}
template <typename CharT>
bool operator==(std::identity_of_t<basic_string_view<CharT>> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) == 0;
}
template <typename CharT>
bool operator!=(basic_string_view<CharT> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) != 0;
}
template <typename CharT>
bool operator!=(std::identity_of_t<basic_string_view<CharT>> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) != 0;
}
template <typename CharT>
bool operator!=(basic_string_view<CharT> x, std::identity_of_t<basic_string_view<CharT>> y) noexcept {
return x.compare(y) != 0;
}
template <typename CharT>
bool operator<(basic_string_view<CharT> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) < 0;
}
template <typename CharT>
bool operator<(std::identity_of_t<basic_string_view<CharT>> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) < 0;
}
template <typename CharT>
bool operator<(basic_string_view<CharT> x, std::identity_of_t<basic_string_view<CharT>> y) noexcept {
return x.compare(y) < 0;
}
template <typename CharT>
bool operator>(basic_string_view<CharT> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) > 0;
}
template <typename CharT>
bool operator>(std::identity_of_t<basic_string_view<CharT>> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) > 0;
}
template <typename CharT>
bool operator>(basic_string_view<CharT> x, std::identity_of_t<basic_string_view<CharT>> y) noexcept {
return x.compare(y) > 0;
}
template <typename CharT>
bool operator<=(basic_string_view<CharT> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) <= 0;
}
template <typename CharT>
bool operator<=(std::identity_of_t<basic_string_view<CharT>> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) <= 0;
}
template <typename CharT>
bool operator<=(basic_string_view<CharT> x, std::identity_of_t<basic_string_view<CharT>> y) noexcept {
return x.compare(y) <= 0;
}
template <typename CharT>
bool operator>=(basic_string_view<CharT> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) >= 0;
}
template <typename CharT>
bool operator>=(std::identity_of_t<basic_string_view<CharT>> x, basic_string_view<CharT> y) noexcept {
return x.compare(y) >= 0;
}
template <typename CharT>
bool operator>=(basic_string_view<CharT> x, std::identity_of_t<basic_string_view<CharT>> y) noexcept {
return x.compare(y) >= 0;
}
} //end of namespace std
#endif