diff --git a/dslings/en/cpp11/15-variadic-templates-0.cpp b/dslings/en/cpp11/15-variadic-templates-0.cpp new file mode 100644 index 0000000..25a8339 --- /dev/null +++ b/dslings/en/cpp11/15-variadic-templates-0.cpp @@ -0,0 +1,49 @@ +// d2mcpp: https://github.com/mcpp-community/d2mcpp +// license: Apache-2.0 +// file: dslings/cpp11/15-variadic-templates-0.cpp +// +// Exercise: cpp11 | 15 - variadic templates | Variadic templates basics +// +// Tips: +// - Variadic templates allow a function to accept any number of arguments +// - In C++11, parameter packs are typically processed via recursion +// - A recursion terminating function (base case) must be defined +// +// Docs: +// - https://en.cppreference.com/w/cpp/language/parameter_pack +// - https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp11/15-variadic-templates.md +// +// Practice discussion: http://forum.d2learn.org/category/20 +// +// Auto-Checker command: +// +// d2x checker variadic-templates +// + +#include +#include + +std::stringstream ss; + +// Define the recursion terminating function +// This is invoked when the parameter pack is empty +D2X_YOUR_ANSWER + +// Define the variadic template function +template +void print(T first, D2X_YOUR_ANSWER args) { + ss << first << " "; + // Recursive call: process the remaining arguments + print(D2X_YOUR_ANSWER); +} + +int main() { + print(1, "hello", 3.14); + + std::string result = ss.str(); + d2x_assert(result == "1 hello 3.14 "); + + D2X_WAIT + + return 0; +} diff --git a/dslings/en/cpp11/15-variadic-templates-1.cpp b/dslings/en/cpp11/15-variadic-templates-1.cpp new file mode 100644 index 0000000..1eaa173 --- /dev/null +++ b/dslings/en/cpp11/15-variadic-templates-1.cpp @@ -0,0 +1,40 @@ +// d2mcpp: https://github.com/mcpp-community/d2mcpp +// license: Apache-2.0 +// file: dslings/cpp11/15-variadic-templates-1.cpp +// +// Exercise: cpp11 | 15 - variadic templates | Variadic template sum +// +// Tips: +// - Implement the sum function using recursion +// - Handle the recursion terminating condition as well +// +// Docs: +// - https://en.cppreference.com/w/cpp/language/parameter_pack +// - https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp11/15-variadic-templates.md +// + +#include + +D2X_YOUR_ANSWER + +template +T sum(T first, D2X_YOUR_ANSWER) { + return first + sum(D2X_YOUR_ANSWER); +} + +int main() { + int res1 = sum(1, 2, 3, 4, 5); + d2x_assert_eq(res1, 15); + + double res2 = sum(1.5, 2.5, 3.0); + d2x_assert(res2 == 7.0); + + // Mixed types + // Note: the return type is determined by the first argument T + int res3 = sum(10, 20.5); + d2x_assert_eq(res3, 30); + + D2X_WAIT + + return 0; +} diff --git a/dslings/en/cpp11/xmake.lua b/dslings/en/cpp11/xmake.lua index 080060d..3f60434 100644 --- a/dslings/en/cpp11/xmake.lua +++ b/dslings/en/cpp11/xmake.lua @@ -55,8 +55,8 @@ target("cpp11-03-trailing-return-type") -- target: cpp11-04-rvalue-references target("cpp11-04-rvalue-references") -set_optimize("none") -add_cxxflags("-fno-elide-constructors") + set_optimize("none") + add_cxxflags("-fno-elide-constructors") add_files("04-rvalue-references.cpp") -- target: cpp11-05-move-semantics @@ -81,7 +81,7 @@ target("cpp11-06-scoped-enums-1") -- target: cpp11-07-constexpr target("cpp11-07-constexpr-0") -add_cxxflags("-Wpedantic -Werror") + add_cxxflags("-Wpedantic -Werror") add_files("07-constexpr-0.cpp") target("cpp11-07-constexpr-1") @@ -90,6 +90,7 @@ target("cpp11-07-constexpr-1") -- target: cpp11-08-literal-type target("cpp11-08-literal-type-0") + set_languages("c++17") -- TODO: optimize it add_files("08-literal-type-0.cpp") target("cpp11-08-literal-type-1") @@ -160,6 +161,14 @@ target("cpp11-14-type-alias-2") target("cpp11-14-type-alias-3") add_files("14-type-alias-3.cpp") +-- target: cpp11-15-variadic-templates + +target("cpp11-15-variadic-templates-0") + add_files("15-variadic-templates-0.cpp") + +target("cpp11-15-variadic-templates-1") + add_files("15-variadic-templates-1.cpp") + -- target: cpp11-16-generalized-unions target("cpp11-16-generalized-unions-0")