Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
- add implementation of three way comparison in c++17 and c++20
  • Loading branch information
mdj2812 committed Jan 13, 2022
0 parents commit 856e7a6
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode/
build/
16 changes: 16 additions & 0 deletions CMakeLists.txt
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
)
3 changes: 3 additions & 0 deletions README.md
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.
5 changes: 5 additions & 0 deletions cpp17/CMakeLists.txt
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
)
91 changes: 91 additions & 0 deletions cpp17/ThreeComp.cpp
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";
}
}
7 changes: 7 additions & 0 deletions cpp17/ThreeComp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace cpp17
{
void threeCompDemo();
void threeCompOperatorOverloadDemo();
}
5 changes: 5 additions & 0 deletions cpp20/CMakeLists.txt
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
)
65 changes: 65 additions & 0 deletions cpp20/ThreeWayComp.cpp
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";
}
}
7 changes: 7 additions & 0 deletions cpp20/ThreeWayComp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace cpp20
{
void threeWayCompDemo();
void threeWayOperatorOverloadDemo();
}
12 changes: 12 additions & 0 deletions main.cpp
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();
}

0 comments on commit 856e7a6

Please sign in to comment.