aboutsummaryrefslogtreecommitdiff
path: root/C++/Threeway Comparison.wiki
blob: 58edd849067785e3b523eac240e24a15ad6406e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Compile with `c++ -std=c++=20 main.cpp`.

Tests the spaceship operator.

{{{c++
#include <iostream>
#include <compare>
#include <sstream>

int str_to_int(const char* s) {
	int ret;
	std::basic_istringstream<char> is(s);
	is >> ret;
	return ret;
}

int main(int argc, char* argv[]) {
	if (argc < 3) {
		std::cerr << "Give at least 2 arguments" << std::endl;
		return 1;
	}

	int a, b;

	a = str_to_int(argv[1]);
	b = str_to_int(argv[2]);

	std::cout << a << " <=> " << b << std::endl;
	std::strong_ordering result = a <=> b;
	if (std::is_lt(result)) {
		std::cout << "LT";
	} else if (std::is_gt(result)) {
		std::cout << "GT";
	} else if (std::is_eq(result)) {
		std::cout << "EQ";
	} else {
		std::cout << "error";
	}

	std::cout << std::endl;
	return 0;
}
}}}