├── .gitignore ├── include ├── fold_expression.h └── variadic_template.h ├── src ├── constexpr_examples │ ├── factorial_constexpr.cpp │ ├── point_constexpr.cpp │ ├── sum_to_n_constexpr.cpp │ ├── template_recursion.cpp │ ├── conditional_constexpr.cpp │ └── fibonacci_numbers.cpp ├── fold_expression.cpp └── variadic_template.cpp ├── CMakeLists.txt ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | *.o 3 | *.exe 4 | *.out 5 | CMakeCache.txt 6 | CMakeFiles/ 7 | cmake_install.cmake 8 | Makefile -------------------------------------------------------------------------------- /include/fold_expression.h: -------------------------------------------------------------------------------- 1 | #ifndef FOLD_EXPRESSION_H 2 | #define FOLD_EXPRESSION_H 3 | 4 | template 5 | void print_fold(Args... args); 6 | 7 | #endif // FOLD_EXPRESSION_H 8 | -------------------------------------------------------------------------------- /src/constexpr_examples/factorial_constexpr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | constexpr int factorial(int n) { 4 | return n <= 1 ? 1 : n * factorial(n - 1); 5 | } 6 | 7 | int main() { 8 | printf("%d\n", factorial(5)); 9 | } -------------------------------------------------------------------------------- /include/variadic_template.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_TEMPLATE_H 2 | #define VARIADIC_TEMPLATE_H 3 | 4 | template 5 | void print(T value); 6 | 7 | template 8 | void print(T firstValue, Args... restValues); 9 | 10 | #endif // VARIADIC_TEMPLATE_H 11 | -------------------------------------------------------------------------------- /src/constexpr_examples/point_constexpr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Point { 5 | int x, y; 6 | 7 | constexpr Point(int x, int y) : x(x), y(y) {} 8 | constexpr float len() const { return sqrt(x * x + y * y); } 9 | }; 10 | 11 | constexpr Point p(3, 4); 12 | 13 | int main() { 14 | printf("%f\n", p.len()); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /src/constexpr_examples/sum_to_n_constexpr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | constexpr int sum_to_n(int n) { 4 | int sum = 0; 5 | for (int i = 1; i <= n; ++i) { 6 | sum += i; 7 | } 8 | return sum; 9 | } 10 | static_assert(sum_to_n(15) == 120, "Something went wrong"); 11 | 12 | int main() { 13 | printf("%d\n", sum_to_n(15)); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /src/constexpr_examples/template_recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | struct Factorial { 5 | static constexpr int value = N * Factorial::value; 6 | }; 7 | 8 | template <> 9 | struct Factorial<0> { 10 | static constexpr int value = 1; 11 | }; 12 | 13 | int main() { 14 | printf("%d\n", Factorial<5>::value); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /src/fold_expression.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "fold_expression.h" 3 | 4 | // Fold expression version (C++17 and later) 5 | template 6 | void print_fold(Args... args) { 7 | ((std::cout << args << std::endl), ...); 8 | } 9 | 10 | int main() { 11 | std::cout << "Using fold expression:" << std::endl; 12 | print_fold(12, 1.56, "Hello, World!"); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /src/constexpr_examples/conditional_constexpr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | void print_type_info(const T& val) { 5 | if constexpr (std::is_integral_v) { 6 | std::cout << "Integer: " << val << std::endl; 7 | } else { 8 | std::cout << "Not an integer" << std::endl; 9 | } 10 | } 11 | 12 | int main() { 13 | print_type_info(15); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /src/constexpr_examples/fibonacci_numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | struct Fibonacci { 5 | static constexpr int value = Fibonacci::value + Fibonacci::value; 6 | }; 7 | 8 | template <> 9 | struct Fibonacci<0> { 10 | static constexpr int value = 0; 11 | }; 12 | 13 | template <> 14 | struct Fibonacci<1> { 15 | static constexpr int value = 1; 16 | }; 17 | 18 | int main() { 19 | printf("%d\n", Fibonacci<10>::value); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /src/variadic_template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_template.h" 3 | 4 | // Base case for recursion 5 | template 6 | void print(T value) { 7 | std::cout << value << std::endl; 8 | } 9 | 10 | // Variadic template function 11 | template 12 | void print(T firstValue, Args... restValues) { 13 | std::cout << firstValue << std::endl; 14 | print(restValues...); 15 | } 16 | 17 | int main() { 18 | std::cout << "Using recursive variadic template:" << std::endl; 19 | print(12, 1.56, "Hello, World!"); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(Metaprogramming) 3 | set(CMAKE_CXX_STANDARD 17) 4 | include_directories(include) 5 | add_executable(template_recursion src/constexpr_examples/template_recursion.cpp) 6 | add_executable(fibonacci_numbers src/constexpr_examples/fibonacci_numbers.cpp) 7 | add_executable(factorial_constexpr src/constexpr_examples/factorial_constexpr.cpp) 8 | add_executable(sum_to_n_constexpr src/constexpr_examples/sum_to_n_constexpr.cpp) 9 | add_executable(conditional_constexpr src/constexpr_examples/conditional_constexpr.cpp) 10 | add_executable(point_constexpr src/constexpr_examples/point_constexpr.cpp) 11 | add_executable(variadic_templates src/variadic_template.cpp) 12 | add_executable(fold_expression src/fold_expression.cpp) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | 13 | 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Variadic Templates in C++ 2 | This project demonstrates the use of variadic templates and fold expressions in C++. 3 | 4 | ## Project Structure 5 | ``` 6 | metaprogramming/ 7 | ├── include/ 8 | │ └── variadic_template.h # Declarations for variadic template utilities 9 | ├── src/ 10 | │ ├── variadic_template.cpp # Recursive and fold-based variadic template printing 11 | │ ├── fold_expression.cpp # Fold expressions: summing and printing variadic args 12 | │ └── constexpr_examples/ 13 | │ ├── conditional_constexpr.cpp # Conditional branching at compile-time 14 | │ ├── factorial_constexpr.cpp # Factorial using constexpr 15 | │ ├── fibonacci_numbers.cpp # Compile-time Fibonacci number generator 16 | │ ├── point_constexpr.cpp # Struct with constexpr constructor and methods 17 | │ ├── sum_to_n_constexpr.cpp # Compute sum from 1 to N at compile-time 18 | │ └── template_recursion.cpp # Classic template recursion for factorial 19 | ├── CMakeLists.txt # Build configuration 20 | ├── .gitignore 21 | ├── LICENSE 22 | └── README.md 23 | ``` 24 | 25 | ## Requirements 26 | - C++17 or later 27 | - CMake 28 | 29 | ## Build Instructions 30 | ```sh 31 | mkdir build && cd build 32 | cmake .. 33 | make 34 | ./variadic_template 35 | ./fold_expression 36 | ``` 37 | 38 | ## License 39 | This project is licensed under the MIT License - see the LICENSE file for details. --------------------------------------------------------------------------------