Skip to content

Commit a80f36e

Browse files
committed
Use C++20 default comparisons for section objects
1 parent 2bb30bc commit a80f36e

File tree

3 files changed

+8
-68
lines changed

3 files changed

+8
-68
lines changed

src/section_data.cpp

+3-24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ struct section_num
1010
{
1111
std::string prefix;
1212
std::vector<int> num;
13+
14+
bool operator==(const section_num&) const = default;
15+
auto operator<=>(const section_num&) const = default;
1316
};
1417

1518
std::istream&
@@ -82,30 +85,6 @@ operator << (std::ostream& os, const section_num& sn)
8285
return os;
8386
}
8487

85-
bool
86-
operator<(const section_num& x, const section_num& y)
87-
{
88-
if (x.prefix < y.prefix)
89-
return true;
90-
else if (y.prefix < x.prefix)
91-
return false;
92-
return x.num < y.num;
93-
}
94-
95-
bool
96-
operator==(const section_num& x, const section_num& y)
97-
{
98-
if (x.prefix != y.prefix)
99-
return false;
100-
return x.num == y.num;
101-
}
102-
103-
bool
104-
operator!=(const section_num& x, const section_num& y)
105-
{
106-
return !(x == y);
107-
}
108-
10988
typedef std::string section_tag;
11089

11190
std::string

src/sections.cpp

+1-31
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,9 @@
44
#include <sstream>
55
#include <iostream>
66
#include <cctype>
7+
#include <tuple>
78
#include <utility>
89

9-
auto lwg::operator<(section_tag const & x, section_tag const & y) noexcept -> bool {
10-
return (x.prefix < y.prefix) ? true
11-
: (y.prefix < x.prefix) ? false
12-
: x.name < y.name;
13-
}
14-
15-
auto lwg::operator==(section_tag const & x, section_tag const & y) noexcept -> bool {
16-
return x.prefix == y.prefix && x.name == y.name;
17-
}
18-
19-
auto lwg::operator!=(section_tag const & x, section_tag const & y) noexcept -> bool {
20-
return !(x == y);
21-
}
22-
2310
auto lwg::operator << (std::ostream& os, section_tag const & tag) -> std::ostream & {
2411
os << '[';
2512
if (!tag.prefix.empty()) { os << tag.prefix << "::"; }
@@ -34,23 +21,6 @@ std::string lwg::as_string(section_tag const & x)
3421
: x.prefix + "::" + x.name;
3522
}
3623

37-
auto lwg::operator < (section_num const & x, section_num const & y) noexcept -> bool {
38-
// prefixes are unique, so there should be no need for a tiebreak.
39-
return (x.prefix < y.prefix) ? true
40-
: (y.prefix < x.prefix) ? false
41-
: x.num < y.num;
42-
}
43-
44-
auto lwg::operator == (section_num const & x, section_num const & y) noexcept -> bool {
45-
return (x.prefix != y.prefix)
46-
? false
47-
: x.num == y.num;
48-
}
49-
50-
auto lwg::operator != (section_num const & x, section_num const & y) noexcept -> bool {
51-
return !(x == y);
52-
}
53-
5424
auto lwg::operator >> (std::istream& is, section_num& sn) -> std::istream & {
5525
sn.prefix.clear();
5626
sn.num.clear();

src/sections.h

+4-13
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,24 @@ struct section_tag
1313
{
1414
std::string prefix; // example: fund.ts.v2
1515
std::string name; // example: meta.logical
16+
bool operator==(const section_tag&) const = default;
17+
auto operator<=>(const section_tag&) const = default;
1618
};
1719

1820
struct section_num {
1921
std::string prefix; // example: fund.ts.v2
2022
std::vector<int> num; // sequence of numbers corresponding to section number
2123
// in relevant doc, e.g,, 17.5.2.1.4.2
24+
bool operator==(const section_num&) const = default;
25+
auto operator<=>(const section_num&) const = default;
2226
};
2327

2428
using section_map = std::map<section_tag, section_num>;
2529

26-
auto operator < (section_tag const & x, section_tag const & y) noexcept -> bool;
27-
auto operator == (section_tag const & x, section_tag const & y) noexcept -> bool;
28-
auto operator != (section_tag const & x, section_tag const & y) noexcept -> bool;
2930
auto operator << (std::ostream & os,
3031
section_tag const & tag) -> std::ostream &; // with square brackets
3132
std::string as_string(section_tag const & x); // without square brackets
3233

33-
auto operator < (section_num const & x, section_num const & y) noexcept -> bool;
34-
// section 'x' sorts before section 'y' if its 'prefix' field lexicographically
35-
// precedes that of 'y', and its 'nun' field lexicographically precedes that
36-
// of 'y' if the prefix fields are equivalent.
37-
38-
auto operator == (section_num const & x, section_num const & y) noexcept -> bool;
39-
auto operator != (section_num const & x, section_num const & y) noexcept -> bool;
40-
// Two 'section_num' objects compare equal if their 'prefix' and 'num' both
41-
// compare equal.
42-
4334
auto operator >> (std::istream & is, section_num & sn) -> std::istream &;
4435
auto operator << (std::ostream & os, section_num const & sn) -> std::ostream &;
4536

0 commit comments

Comments
 (0)