From a4948ef41019417ecbd1c36244a4b431adfecd9d Mon Sep 17 00:00:00 2001 From: Fries Date: Sun, 25 Jun 2023 18:11:41 -0700 Subject: [PATCH] add a few new programs related to templates i made new programs related to lambdas, and various things you can do with templates like generic functions and overriding the () operator meaning you can call a class object like a function. --- src/CMakeLists.txt | 3 +++ src/FunctionObjects.cc | 54 ++++++++++++++++++++++++++++++++++++++++ src/FunctionTemplates.cc | 28 +++++++++++++++++++++ src/lambdas.cc | 23 +++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 src/FunctionObjects.cc create mode 100644 src/FunctionTemplates.cc create mode 100644 src/lambdas.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ffcc1e7..66898c2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,3 +20,6 @@ add_executable(vector vector.cc) add_executable(Vector2 Vector2.cc) add_executable(TemplateVector TemplateVector.cc) add_executable(buffer buffer.cc) +add_executable(FunctionTemplates FunctionTemplates.cc) +add_executable(FunctionObjects FunctionObjects.cc) +add_executable(lambdas lambdas.cc) diff --git a/src/FunctionObjects.cc b/src/FunctionObjects.cc new file mode 100644 index 0000000..5c4b220 --- /dev/null +++ b/src/FunctionObjects.cc @@ -0,0 +1,54 @@ +// you can override the () operator meaning you can call class objects like functions. + +#include +#include +#include + +template +class LessThan { +private: + const T val; + +public: + LessThan(const T& v) + : val { v } + { + } + + bool operator()(const T& x) const + { + return x < val; + } +}; + +template +class GreaterThan { +private: + const T val; + +public: + GreaterThan(const T& v) + : val { v } + { + } + + bool operator()(const T& x) const + { + return x > val; + } +}; + +int main() +{ + LessThan i1(21); + LessThan i2(21.420); + + GreaterThan i3(21); + GreaterThan i4(21.420); + + std::cout << std::format("less than 21: {}", i1(1)) << std::endl; + std::cout << std::format("less than 21.420: {}", i2(420.21)) << std::endl; + + std::cout << std::format("greater than 21: {}", i3(1)) << std::endl; + std::cout << std::format("greater than 21.420: {}", i4(420.21)) << std::endl; +} diff --git a/src/FunctionTemplates.cc b/src/FunctionTemplates.cc new file mode 100644 index 0000000..783cbbf --- /dev/null +++ b/src/FunctionTemplates.cc @@ -0,0 +1,28 @@ +#include +#include +#include + +template +T sum(const T& sum1, const T& sum2) +{ + return sum1 + sum2; +} + +void print_number(const std::string& stringName, const auto& value) +{ + std::cout << std::format("{} = {}", stringName, value) << std::endl; +} + +int main() +{ + double i1 = sum(1.4, 1.2); + int i2 = sum(1, 1); + + auto i3 = sum(2, 2); + auto i4 = sum(4.4, 4.2); + + print_number("i1", i1); + print_number("i2", i2); + print_number("i3", i3); + print_number("i4", i4); +} diff --git a/src/lambdas.cc b/src/lambdas.cc new file mode 100644 index 0000000..ba32b0f --- /dev/null +++ b/src/lambdas.cc @@ -0,0 +1,23 @@ +#include +#include + +template +bool less_than(const T& i) +{ + return [&]() { + return i < 21; + }(); +} + +auto greater_than = [](const double i) { + return i > 21.420; +}; + +int main() +{ + std::cout << std::format("less than 21 : {}", less_than(20)) << std::endl; + std::cout << std::format("less than 21 : {}", less_than(22)) << std::endl;; + + std::cout << std::format("greater than 21 : {}", less_than(22)) << std::endl; + std::cout << std::format("greater than 21 : {}", less_than(20)) << std::endl; +}