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.
This commit is contained in:
parent
3dcb5197cb
commit
a4948ef410
4 changed files with 108 additions and 0 deletions
|
@ -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)
|
||||
|
|
54
src/FunctionObjects.cc
Normal file
54
src/FunctionObjects.cc
Normal file
|
@ -0,0 +1,54 @@
|
|||
// you can override the () operator meaning you can call class objects like functions.
|
||||
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
template <typename T>
|
||||
class LessThan {
|
||||
private:
|
||||
const T val;
|
||||
|
||||
public:
|
||||
LessThan(const T& v)
|
||||
: val { v }
|
||||
{
|
||||
}
|
||||
|
||||
bool operator()(const T& x) const
|
||||
{
|
||||
return x < val;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
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<int> i1(21);
|
||||
LessThan<double> i2(21.420);
|
||||
|
||||
GreaterThan<int> i3(21);
|
||||
GreaterThan<double> 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;
|
||||
}
|
28
src/FunctionTemplates.cc
Normal file
28
src/FunctionTemplates.cc
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include <format>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
template <typename T>
|
||||
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<int>(2, 2);
|
||||
auto i4 = sum<double>(4.4, 4.2);
|
||||
|
||||
print_number("i1", i1);
|
||||
print_number("i2", i2);
|
||||
print_number("i3", i3);
|
||||
print_number("i4", i4);
|
||||
}
|
23
src/lambdas.cc
Normal file
23
src/lambdas.cc
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <format>
|
||||
#include <iostream>
|
||||
|
||||
template <typename T>
|
||||
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 <lambda edition>: {}", less_than(20)) << std::endl;
|
||||
std::cout << std::format("less than 21 <lambda edition>: {}", less_than(22)) << std::endl;;
|
||||
|
||||
std::cout << std::format("greater than 21 <lambda edition>: {}", less_than(22)) << std::endl;
|
||||
std::cout << std::format("greater than 21 <lambda edition>: {}", less_than(20)) << std::endl;
|
||||
}
|
Loading…
Reference in a new issue