From 856e7a6659b994d2c973d442f31214d0825e904a Mon Sep 17 00:00:00 2001 From: MA Dingjie Date: Thu, 13 Jan 2022 12:29:35 +0800 Subject: [PATCH] Initial Commit - add implementation of three way comparison in c++17 and c++20 --- .gitignore | 2 + CMakeLists.txt | 16 ++++++++ README.md | 3 ++ cpp17/CMakeLists.txt | 5 +++ cpp17/ThreeComp.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++ cpp17/ThreeComp.hpp | 7 ++++ cpp20/CMakeLists.txt | 5 +++ cpp20/ThreeWayComp.cpp | 65 ++++++++++++++++++++++++++++++ cpp20/ThreeWayComp.hpp | 7 ++++ main.cpp | 12 ++++++ 10 files changed, 213 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 cpp17/CMakeLists.txt create mode 100644 cpp17/ThreeComp.cpp create mode 100644 cpp17/ThreeComp.hpp create mode 100644 cpp20/CMakeLists.txt create mode 100644 cpp20/ThreeWayComp.cpp create mode 100644 cpp20/ThreeWayComp.hpp create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e524d79 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..efa9ad5 --- /dev/null +++ b/CMakeLists.txt @@ -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 +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..862327b --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/cpp17/CMakeLists.txt b/cpp17/CMakeLists.txt new file mode 100644 index 0000000..fae1cda --- /dev/null +++ b/cpp17/CMakeLists.txt @@ -0,0 +1,5 @@ +set(CMAKE_CXX_STANDARD 17) + +add_library(cpp17_demo_lib STATIC + ThreeComp.cpp +) \ No newline at end of file diff --git a/cpp17/ThreeComp.cpp b/cpp17/ThreeComp.cpp new file mode 100644 index 0000000..f8c9d1d --- /dev/null +++ b/cpp17/ThreeComp.cpp @@ -0,0 +1,91 @@ +#include "ThreeComp.hpp" + +#include + +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"; + } +} \ No newline at end of file diff --git a/cpp17/ThreeComp.hpp b/cpp17/ThreeComp.hpp new file mode 100644 index 0000000..cfa2c21 --- /dev/null +++ b/cpp17/ThreeComp.hpp @@ -0,0 +1,7 @@ +#pragma once + +namespace cpp17 +{ + void threeCompDemo(); + void threeCompOperatorOverloadDemo(); +} \ No newline at end of file diff --git a/cpp20/CMakeLists.txt b/cpp20/CMakeLists.txt new file mode 100644 index 0000000..836b28f --- /dev/null +++ b/cpp20/CMakeLists.txt @@ -0,0 +1,5 @@ +set(CMAKE_CXX_STANDARD 20) + +add_library(cpp20_demo_lib STATIC + ThreeWayComp.cpp +) diff --git a/cpp20/ThreeWayComp.cpp b/cpp20/ThreeWayComp.cpp new file mode 100644 index 0000000..d29a860 --- /dev/null +++ b/cpp20/ThreeWayComp.cpp @@ -0,0 +1,65 @@ +#include "ThreeWayComp.hpp" + +#include +#include + +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"; + } +} diff --git a/cpp20/ThreeWayComp.hpp b/cpp20/ThreeWayComp.hpp new file mode 100644 index 0000000..90c7951 --- /dev/null +++ b/cpp20/ThreeWayComp.hpp @@ -0,0 +1,7 @@ +#pragma once + +namespace cpp20 +{ + void threeWayCompDemo(); + void threeWayOperatorOverloadDemo(); +} \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..73d5677 --- /dev/null +++ b/main.cpp @@ -0,0 +1,12 @@ +#include "cpp17/ThreeComp.hpp" +#include "cpp20/ThreeWayComp.hpp" + +#include + +int main(int, char **) +{ + cpp17::threeCompDemo(); + cpp20::threeWayCompDemo(); + cpp17::threeCompOperatorOverloadDemo(); + cpp20::threeWayOperatorOverloadDemo(); +}