Created
January 22, 2023 03:30
-
-
Save uemuraj/5114d92caf4154661670516b7d683917 to your computer and use it in GitHub Desktop.
三方比較演算子と一貫比較。これはらくちん。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pch.h" | |
#include <compare> | |
#include <iostream> | |
// https://cpprefjp.github.io/lang/cpp20/consistent_comparison.html | |
struct Hoge | |
{ | |
int a, b; | |
auto operator<=>(const Hoge &) const = default; | |
friend std::ostream & operator<<(std::ostream & out, const Hoge & hoge) | |
{ | |
return out << "a=" << hoge.a << ", b=" << hoge.b; | |
} | |
}; | |
TEST(ThreeWayComparison, Hoge) | |
{ | |
Hoge hogeA{ 1,2 }; | |
Hoge hogeB{ 1,2 }; | |
Hoge hogeC{ 1,3 }; | |
EXPECT_EQ(hogeA, hogeB); | |
EXPECT_NE(hogeA, hogeC); | |
EXPECT_LT(hogeA, hogeC); | |
EXPECT_GT(hogeC, hogeA); | |
EXPECT_LE(hogeA, hogeB); | |
EXPECT_GE(hogeB, hogeA); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment