-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add implementation of three way comparison in c++17 and c++20
- Loading branch information
0 parents
commit 856e7a6
Showing
10 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode/ | ||
build/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cmake_minimum_required(VERSION 3.17.0) | ||
project(cpp20_demo) | ||
|
||
include(CTest) | ||
enable_testing() | ||
|
||
add_subdirectory(cpp17) | ||
add_subdirectory(cpp20) | ||
|
||
add_executable(cpp20_demo main.cpp) | ||
|
||
target_link_libraries(cpp20_demo | ||
PUBLIC | ||
cpp17_demo_lib | ||
cpp20_demo_lib | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# cpp20_demo | ||
|
||
Demo of new features of standard C++20. This repo would contain some comparison of implementations between C++20 and its prior standard C++17. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
add_library(cpp17_demo_lib STATIC | ||
ThreeComp.cpp | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "ThreeComp.hpp" | ||
|
||
#include <iostream> | ||
|
||
namespace cpp17 | ||
{ | ||
void threeCompDemo() | ||
{ | ||
constexpr double foo = -0.0; | ||
constexpr double bar = 0.0; | ||
|
||
std::cout << "cpp17: "; | ||
|
||
if (foo < bar) | ||
{ | ||
std::cout << "-0 is less than 0"; | ||
} | ||
else if (foo > bar) | ||
{ | ||
std::cout << "-0 is greater than 0"; | ||
} | ||
else // (foo == bar) | ||
{ | ||
std::cout << "-0 and 0 are equal"; | ||
} | ||
|
||
std::cout << "\n"; | ||
} | ||
|
||
struct Point | ||
{ | ||
int x; | ||
int y; | ||
bool operator<(const Point &other) const | ||
{ | ||
if (x < other.x) | ||
{ | ||
return true; | ||
} | ||
|
||
if (y >= other.y) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
bool operator==(const Point &other) const | ||
{ | ||
return (x == other.x) && (y == other.y); | ||
} | ||
bool operator>(const Point &other) const | ||
{ | ||
if (x > other.x) | ||
{ | ||
return true; | ||
} | ||
|
||
if (y <= other.y) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
}; | ||
// compiler generates all six two-way comparison operators | ||
|
||
void threeCompOperatorOverloadDemo() | ||
{ | ||
constexpr Point A{1, 1}; | ||
constexpr Point B{2, 1}; | ||
|
||
std::cout << "cp20: "; | ||
|
||
if (A < B) | ||
{ | ||
std::cout << "A is less than B"; | ||
} | ||
else if (A > B) | ||
{ | ||
std::cout << "A is greater than B"; | ||
} | ||
else // (A == B) | ||
{ | ||
std::cout << "A and B are equal"; | ||
} | ||
|
||
std::cout << "\n"; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
namespace cpp17 | ||
{ | ||
void threeCompDemo(); | ||
void threeCompOperatorOverloadDemo(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
set(CMAKE_CXX_STANDARD 20) | ||
|
||
add_library(cpp20_demo_lib STATIC | ||
ThreeWayComp.cpp | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "ThreeWayComp.hpp" | ||
|
||
#include <compare> | ||
#include <iostream> | ||
|
||
namespace cpp20 | ||
{ | ||
void threeWayCompDemo() | ||
{ | ||
constexpr double foo = -0.0; | ||
constexpr double bar = 0.0; | ||
|
||
constexpr auto res = (foo <=> bar); | ||
|
||
std::cout << "cp20: "; | ||
|
||
if (res < 0) | ||
{ | ||
std::cout << "-0 is less than 0"; | ||
} | ||
else if (res > 0) | ||
{ | ||
std::cout << "-0 is greater than 0"; | ||
} | ||
else // (res == 0) | ||
{ | ||
std::cout << "-0 and 0 are equal"; | ||
} | ||
|
||
std::cout << "\n"; | ||
} | ||
|
||
struct Point | ||
{ | ||
int x; | ||
int y; | ||
auto operator<=>(const Point &) const = default; | ||
}; | ||
// compiler generates all six two-way comparison operators | ||
|
||
void threeWayOperatorOverloadDemo() | ||
{ | ||
constexpr Point A{1, 1}; | ||
constexpr Point B{2, 1}; | ||
|
||
constexpr auto res = (A <=> B); | ||
|
||
std::cout << "cp20: "; | ||
|
||
if (res < 0) | ||
{ | ||
std::cout << "A is less than B"; | ||
} | ||
else if (res > 0) | ||
{ | ||
std::cout << "A is greater than B"; | ||
} | ||
else // (res == 0) | ||
{ | ||
std::cout << "A and B are equal"; | ||
} | ||
|
||
std::cout << "\n"; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
namespace cpp20 | ||
{ | ||
void threeWayCompDemo(); | ||
void threeWayOperatorOverloadDemo(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "cpp17/ThreeComp.hpp" | ||
#include "cpp20/ThreeWayComp.hpp" | ||
|
||
#include <iostream> | ||
|
||
int main(int, char **) | ||
{ | ||
cpp17::threeCompDemo(); | ||
cpp20::threeWayCompDemo(); | ||
cpp17::threeCompOperatorOverloadDemo(); | ||
cpp20::threeWayOperatorOverloadDemo(); | ||
} |