From 3a76b4eb885d957705bd40089b206c4bda9ab4c4 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 4 Nov 2023 18:20:13 +0100 Subject: [PATCH] feat: variadic templates --- chapters/chapter_02/CMakeLists.txt | 3 +- .../chapter_02/include/variadic_templates.h | 10 ++++++ .../chapter_02/src/variadic_templates.cpp | 36 +++++++++++++++++++ src/main.cpp | 2 ++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 chapters/chapter_02/include/variadic_templates.h create mode 100644 chapters/chapter_02/src/variadic_templates.cpp diff --git a/chapters/chapter_02/CMakeLists.txt b/chapters/chapter_02/CMakeLists.txt index f656efe..5972957 100644 --- a/chapters/chapter_02/CMakeLists.txt +++ b/chapters/chapter_02/CMakeLists.txt @@ -7,7 +7,8 @@ add_library( src/null.cpp src/range_based_for.cpp src/structured_binding.cpp - src/type_inference.cpp) + src/type_inference.cpp + src/variadic_templates.cpp) target_compile_features(chapter_02 PUBLIC cxx_std_20) diff --git a/chapters/chapter_02/include/variadic_templates.h b/chapters/chapter_02/include/variadic_templates.h new file mode 100644 index 0000000..7ecac63 --- /dev/null +++ b/chapters/chapter_02/include/variadic_templates.h @@ -0,0 +1,10 @@ +#pragma once + +#include "chapter_interface.h" + +class VariadicTemplates : public ChapterTest { + virtual void run() override; + virtual const char *name() const override { + return "Variadic templates test"; + } +}; diff --git a/chapters/chapter_02/src/variadic_templates.cpp b/chapters/chapter_02/src/variadic_templates.cpp new file mode 100644 index 0000000..6edab5f --- /dev/null +++ b/chapters/chapter_02/src/variadic_templates.cpp @@ -0,0 +1,36 @@ +#include "variadic_templates.h" +#include + +template void my_recursive_printf(T value) { + std::cout << value << std::endl; +} + +template +void my_recursive_printf(T value, Ts... args) { + std::cout << value; + my_recursive_printf(args...); +} + +template +void my_expanded_printf(T value, Ts... args) { + std::cout << value; + + if constexpr (sizeof...(args) > 0) { + my_expanded_printf(args...); + } + + std::cout << std::endl; +} + +template auto sum(T... t) { return (t + ...); } + +void VariadicTemplates::run() { + std::cout << "my_recursive_printf(1, 2, \"3\", 4.5, \"hello\"): "; + my_recursive_printf(1, 2, "3", 4.5, "hello"); + + std::cout << "my_expanded_printf(1, 2, \"3\", 4.5, \"hello\"): "; + my_expanded_printf(1, 2, "3", 4.5, "hello"); + + std::cout << "sum(1, 2, 3, 4, 5, 6, 7): " << sum(1, 2, 3, 4, 5, 6, 7) + << std::endl; +} diff --git a/src/main.cpp b/src/main.cpp index ee52b78..07fcc69 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include "range_based_for.h" #include "structured_binding.h" #include "type_inference.h" +#include "variadic_templates.h" #include "version.h" int main(int argc, char **argv) { @@ -28,6 +29,7 @@ int main(int argc, char **argv) { chapter_2_tests.emplace_back(std::make_unique()); chapter_2_tests.emplace_back(std::make_unique()); chapter_2_tests.emplace_back(std::make_unique()); + chapter_2_tests.emplace_back(std::make_unique()); for (auto &test : chapter_2_tests) { std::cout << "\n" << test->name() << ":" << std::endl;