Skip to content

Instantly share code, notes, and snippets.

@uemuraj
Created January 22, 2023 03:30
Show Gist options
  • Save uemuraj/5114d92caf4154661670516b7d683917 to your computer and use it in GitHub Desktop.
Save uemuraj/5114d92caf4154661670516b7d683917 to your computer and use it in GitHub Desktop.
三方比較演算子と一貫比較。これはらくちん。
#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