From 77fc239a5b3239ad35b40b5850c2ffc6aeeb6345 Mon Sep 17 00:00:00 2001 From: Amar Mahmutbegovic Date: Sun, 11 May 2025 21:59:37 +0200 Subject: [PATCH] add Chapter08 --- Chapter08/template_function.cpp | 16 ++++++++ Chapter08/template_function_point.cpp | 29 ++++++++++++++ Chapter08/template_specialization.cpp | 34 ++++++++++++++++ Chapter08/tmpt_concepts.cpp | 44 +++++++++++++++++++++ Chapter08/tmpt_crtp.cpp | 56 +++++++++++++++++++++++++++ Chapter08/tmpt_enable_if.cpp | 34 ++++++++++++++++ 6 files changed, 213 insertions(+) create mode 100644 Chapter08/template_function.cpp create mode 100644 Chapter08/template_function_point.cpp create mode 100644 Chapter08/template_specialization.cpp create mode 100644 Chapter08/tmpt_concepts.cpp create mode 100644 Chapter08/tmpt_crtp.cpp create mode 100644 Chapter08/tmpt_enable_if.cpp diff --git a/Chapter08/template_function.cpp b/Chapter08/template_function.cpp new file mode 100644 index 0000000..5d726c8 --- /dev/null +++ b/Chapter08/template_function.cpp @@ -0,0 +1,16 @@ +#include + +template +T add(T a, T b) { + return a + b; +} + +int main() { + int result_int = add(1, 4); + float result_float = add(1.11f, 1.91f); + + printf("result_int = %d\r\n", result_int); + printf("result_float = %.2f\r\n", result_float); + + return 0; +} \ No newline at end of file diff --git a/Chapter08/template_function_point.cpp b/Chapter08/template_function_point.cpp new file mode 100644 index 0000000..61784c5 --- /dev/null +++ b/Chapter08/template_function_point.cpp @@ -0,0 +1,29 @@ +#include + +struct point { + int x; + int y; + + point operator+(const point& other) const { + return point{x + other.x, y + other.y}; + } + + void print() { + printf("x = %d, y = %d\r\n", x, y); + } +}; + +template +T add(T a, T b) { + return a + b; +} + +int main() { + + point a{1, 2}; + point b{2, 1}; + + auto c = add(a, b); + c.print(); + return 0; +} \ No newline at end of file diff --git a/Chapter08/template_specialization.cpp b/Chapter08/template_specialization.cpp new file mode 100644 index 0000000..3c177c7 --- /dev/null +++ b/Chapter08/template_specialization.cpp @@ -0,0 +1,34 @@ +#include + +struct point { + int x; + int y; + + point operator+(const point& other) const { + return point{x + other.x, y + other.y}; + } + + void print() { + printf("x = %d, y = %d\r\n", x, y); + } +}; + +template +T add(T a, T b) { + return a + b; +} + +template<> +point add(point a, point b) { + return point{a.x+b.x+1, a.y+b.y+1}; +} + +int main() { + + point a{1, 2}; + point b{2, 1}; + + auto c = add(a, b); + c.print(); + return 0; +} \ No newline at end of file diff --git a/Chapter08/tmpt_concepts.cpp b/Chapter08/tmpt_concepts.cpp new file mode 100644 index 0000000..7bcfd45 --- /dev/null +++ b/Chapter08/tmpt_concepts.cpp @@ -0,0 +1,44 @@ +#include +#include + +struct point { + int x; + int y; + + point operator+(const point& other) const { + return point{x + other.x, y + other.y}; + } + + void print() { + printf("x = %d, y = %d\r\n", x, y); + } +}; + +template +concept Arithmetic = std::is_arithmetic_v; + + +template +T add(T a, T b) { + + T result = a + b; + if constexpr (std::is_integral_v) { + printf("%d + %d = %d\r\n", a, b, result); + } else if constexpr (std::is_floating_point_v) { + printf("%.2f + %.2f = %.2f\r\n", a, b, result); + } + + return a + b; +} + +int main() { + + auto a = add(1, 2); // OK + auto b = add(1.1, 2.1); // OK + + point p_a{1, 2}; + point p_b{2, 1}; + auto c = add(p_a, p_b); // compile-error + + return 0; +} \ No newline at end of file diff --git a/Chapter08/tmpt_crtp.cpp b/Chapter08/tmpt_crtp.cpp new file mode 100644 index 0000000..e3aa536 --- /dev/null +++ b/Chapter08/tmpt_crtp.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +template +class uart_interface { +public: + void init(std::uint32_t baudrate = 9600) { + static_cast(this)->initImpl(baudrate); + } + + void write(std::span data) { + static_cast(this)->writeImpl(data); + } +}; + +class uart_stm32 : public uart_interface { +public: + void initImpl(std::uint32_t baudrate = 9600) { + printf("uart_stm32::init: setting baudrate to %d\r\n", baudrate); + } + + void writeImpl(std::span data) { + printf("uart_stm32::write: "); + for(auto ch: data) { + putc(ch, stdout); + } + } +}; + +template +concept TheUart = std::derived_from>; + +template +class gsm_lib{ +public: + gsm_lib(T &u) : uart_(u) {} + + void init() { + printf("gsm_lib::init: sending AT command\r\n"); + uart_.write("AT"); + } + +private: + T &uart_; +}; + +int main() { + uart_stm32 uart_stm32_obj; + uart_stm32_obj.init(115200); + + gsm_lib gsm(uart_stm32_obj); + gsm.init(); + + return 0; +} \ No newline at end of file diff --git a/Chapter08/tmpt_enable_if.cpp b/Chapter08/tmpt_enable_if.cpp new file mode 100644 index 0000000..d2c9853 --- /dev/null +++ b/Chapter08/tmpt_enable_if.cpp @@ -0,0 +1,34 @@ +#include +#include + +struct point { + int x; + int y; + + point operator+(const point& other) const { + return point{x + other.x, y + other.y}; + } + + void print() { + printf("x = %d, y = %d\r\n", x, y); + } +}; + +template +std::enable_if::value, T>::type +add(T a, T b) { + return a + b; +} + +int main() { + + auto a = add(1, 2); // OK + auto b = add(1.1, 2.1); // OK + + point p_a{1, 2}; + point p_b{2, 1}; + auto c = add(p_a, p_b); // compile-error + + return 0; + +} \ No newline at end of file