add Chapter08

This commit is contained in:
Amar Mahmutbegovic
2025-05-11 21:59:37 +02:00
parent 0ea2c43f45
commit 77fc239a5b
6 changed files with 213 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
#include <cstdio>
template<typename T>
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;
}

View File

@@ -0,0 +1,29 @@
#include <cstdio>
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<typename T>
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;
}

View File

@@ -0,0 +1,34 @@
#include <cstdio>
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<typename T>
T add(T a, T b) {
return a + b;
}
template<>
point add<point>(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;
}

View File

@@ -0,0 +1,44 @@
#include <cstdio>
#include <type_traits>
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<typename T>
concept Arithmetic = std::is_arithmetic_v<T>;
template<Arithmetic T>
T add(T a, T b) {
T result = a + b;
if constexpr (std::is_integral_v<T>) {
printf("%d + %d = %d\r\n", a, b, result);
} else if constexpr (std::is_floating_point_v<T>) {
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;
}

56
Chapter08/tmpt_crtp.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include <span>
#include <cstdio>
#include <cstdint>
template<typename U>
class uart_interface {
public:
void init(std::uint32_t baudrate = 9600) {
static_cast<U*>(this)->initImpl(baudrate);
}
void write(std::span<const char> data) {
static_cast<U*>(this)->writeImpl(data);
}
};
class uart_stm32 : public uart_interface<uart_stm32> {
public:
void initImpl(std::uint32_t baudrate = 9600) {
printf("uart_stm32::init: setting baudrate to %d\r\n", baudrate);
}
void writeImpl(std::span<const char> data) {
printf("uart_stm32::write: ");
for(auto ch: data) {
putc(ch, stdout);
}
}
};
template<typename T>
concept TheUart = std::derived_from<T, uart_interface<T>>;
template<TheUart T>
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;
}

View File

@@ -0,0 +1,34 @@
#include <cstdio>
#include <type_traits>
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<typename T>
std::enable_if<std::is_arithmetic<T>::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;
}