From 744aa669920ecb5b598b3eac73ae6dccc9dfc9e8 Mon Sep 17 00:00:00 2001 From: Sandipan Mohanty <s.mohanty@fz-juelich.de> Date: Mon, 22 May 2023 11:40:40 +0200 Subject: [PATCH] Collect examples for chapter 3 --- {day3 => chapter_03}/examples/CMakeLists.txt | 0 chapter_03/examples/fold_xpr_demo2.cc | 25 ++++++++++++ chapter_03/examples/fold_xpr_demo3.cc | 20 ++++++++++ chapter_03/examples/fold_xpr_demo4.cc | 32 +++++++++++++++ chapter_03/examples/foldex.cc | 27 +++++++++++++ chapter_03/examples/foldex_3.cc | 13 +++++++ .../examples/gcd_w_concepts.cc | 0 .../examples/generic_func1.cc | 0 .../examples/generic_func2.cc | 0 .../examples/generic_func3.cc | 0 .../examples/generic_func4.cc | 0 {day3 => chapter_03}/examples/no_textsub.cc | 0 .../examples/overload_w_concepts.cc | 0 chapter_03/examples/print_tuple.cc | 33 ++++++++++++++++ chapter_03/examples/print_tuple_cxx17.cc | 32 +++++++++++++++ chapter_03/examples/print_tuple_foldex.cc | 31 +++++++++++++++ .../examples/static_assert0.cc | 0 .../examples/static_assert1.cc | 0 .../examples/static_assert2.cc | 0 .../examples/template_intro.cc | 0 chapter_03/examples/variadic_1.cc | 39 +++++++++++++++++++ chapter_03/examples/variadic_2.cc | 27 +++++++++++++ chapter_03/examples/variadic_3.cc | 36 +++++++++++++++++ {day4 => chapter_03}/examples/variadic_3b.cc | 0 24 files changed, 315 insertions(+) rename {day3 => chapter_03}/examples/CMakeLists.txt (100%) create mode 100644 chapter_03/examples/fold_xpr_demo2.cc create mode 100644 chapter_03/examples/fold_xpr_demo3.cc create mode 100644 chapter_03/examples/fold_xpr_demo4.cc create mode 100644 chapter_03/examples/foldex.cc create mode 100644 chapter_03/examples/foldex_3.cc rename {day3 => chapter_03}/examples/gcd_w_concepts.cc (100%) rename {day3 => chapter_03}/examples/generic_func1.cc (100%) rename {day3 => chapter_03}/examples/generic_func2.cc (100%) rename {day3 => chapter_03}/examples/generic_func3.cc (100%) rename {day3 => chapter_03}/examples/generic_func4.cc (100%) rename {day3 => chapter_03}/examples/no_textsub.cc (100%) rename {day3 => chapter_03}/examples/overload_w_concepts.cc (100%) create mode 100644 chapter_03/examples/print_tuple.cc create mode 100644 chapter_03/examples/print_tuple_cxx17.cc create mode 100644 chapter_03/examples/print_tuple_foldex.cc rename {day3 => chapter_03}/examples/static_assert0.cc (100%) rename {day3 => chapter_03}/examples/static_assert1.cc (100%) rename {day3 => chapter_03}/examples/static_assert2.cc (100%) rename {day3 => chapter_03}/examples/template_intro.cc (100%) create mode 100644 chapter_03/examples/variadic_1.cc create mode 100644 chapter_03/examples/variadic_2.cc create mode 100644 chapter_03/examples/variadic_3.cc rename {day4 => chapter_03}/examples/variadic_3b.cc (100%) diff --git a/day3/examples/CMakeLists.txt b/chapter_03/examples/CMakeLists.txt similarity index 100% rename from day3/examples/CMakeLists.txt rename to chapter_03/examples/CMakeLists.txt diff --git a/chapter_03/examples/fold_xpr_demo2.cc b/chapter_03/examples/fold_xpr_demo2.cc new file mode 100644 index 0000000..6750d64 --- /dev/null +++ b/chapter_03/examples/fold_xpr_demo2.cc @@ -0,0 +1,25 @@ +#include <iostream> +#include <string> +#include <vector> + +auto add_up(auto... args) +{ + return (args + ...); +} + +template <typename T> +void push_back(std::vector<T> &v, auto ... args) { + (v.push_back(args), ...); +} + +auto main() -> int +{ + using namespace std; + string firstname{ "Steve" }, lastname{ "Rogers" }; + cout << add_up(1, 2, 3, 4, 5) << "\n"; + cout << add_up(firstname, " ", lastname) << "\n"; + + std::vector<double> q{0.0}; + push_back(q, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8); + for (auto d : q) cout << d << "\n"; +} diff --git a/chapter_03/examples/fold_xpr_demo3.cc b/chapter_03/examples/fold_xpr_demo3.cc new file mode 100644 index 0000000..2c7f4c3 --- /dev/null +++ b/chapter_03/examples/fold_xpr_demo3.cc @@ -0,0 +1,20 @@ +#include <vector> +#include <iostream> +#include <algorithm> +#include <ranges> +#include <string> +#include "print_tuple.hh" + +auto max_of_multiple(auto ... containers) +{ + return std::make_tuple(std::ranges::max(containers) ...); +} + +auto main() -> int +{ + std::vector v1{8.2, 84., 9.1, 33.1, 9.33, 8.2, 8.3}; + std::vector v2{9,1,2,8,3,1,4,2,0,8,1}; + std::vector<std::string> v3{"Compact", "code", "with", "fold", "expressions"}; + std::cout << max_of_multiple(v1, v2, v3) << "\n"; +} + diff --git a/chapter_03/examples/fold_xpr_demo4.cc b/chapter_03/examples/fold_xpr_demo4.cc new file mode 100644 index 0000000..bd91600 --- /dev/null +++ b/chapter_03/examples/fold_xpr_demo4.cc @@ -0,0 +1,32 @@ +#include <vector> +#include <iostream> +#include <ranges> +#include <algorithm> +#include "range_output.hh" + +auto conv(const std::vector<double>& inp, auto ... shift) +{ + namespace sr = std::ranges; + namespace sv = std::views; + std::vector<double> out(inp.size(), 0.); + auto res_exp = sv::iota(0, static_cast<int>(inp.size())) + | sv::transform([inp, shift...](auto index){ + auto S = inp.size(); + return (inp[(index + shift) > 0 ? (index + shift) % S : S + (index + shift) % S] + ...) + / (sizeof ... (shift)); + }); + + sr::copy(res_exp, out.begin()); + return out; +} + +auto main() -> int +{ + std::vector v(21UL, 0.); + v[10] = 1.0; + for (auto i = 0UL; i < 10; ++i) { + v = conv(v, 0, 1, 2); + std::cout << "After round " << i << ", v = " << output::comma_separated << v << "\n"; + } +} + diff --git a/chapter_03/examples/foldex.cc b/chapter_03/examples/foldex.cc new file mode 100644 index 0000000..dfc8ed5 --- /dev/null +++ b/chapter_03/examples/foldex.cc @@ -0,0 +1,27 @@ +#include <iostream> +#include <string> +#include <vector> + +template <typename... Args> +auto add_up(Args... args) +{ + return (args + ...); +} + +template <typename T, typename... Args> +void push_back(std::vector<T> &v, Args ... args) { + (v.push_back(args), ...); +} + +auto main() -> int +{ + using namespace std; + string firstname{ "Stephen" }, lastname{ "Hawking" }; + cout << add_up(1, 2, 3, 4, 5) << "\n"; + cout << add_up(firstname, " ", lastname) << "\n"; + + std::vector<double> q{0.0}; + push_back(q, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8); + for (auto d : q) cout << d << "\n"; +} + diff --git a/chapter_03/examples/foldex_3.cc b/chapter_03/examples/foldex_3.cc new file mode 100644 index 0000000..fad515b --- /dev/null +++ b/chapter_03/examples/foldex_3.cc @@ -0,0 +1,13 @@ +// examples/foldex_3.cc +#include <algorithm> +template <class First, class... Args> +auto min(First first, Args... args) +{ + First retval = first; + ((retval = std::min(retval, args)), ...); + return retval; +} +int main() +{ + return min(8, 3, 4, 7, 2, 7) + min(2, 3, 9, 1); +} diff --git a/day3/examples/gcd_w_concepts.cc b/chapter_03/examples/gcd_w_concepts.cc similarity index 100% rename from day3/examples/gcd_w_concepts.cc rename to chapter_03/examples/gcd_w_concepts.cc diff --git a/day3/examples/generic_func1.cc b/chapter_03/examples/generic_func1.cc similarity index 100% rename from day3/examples/generic_func1.cc rename to chapter_03/examples/generic_func1.cc diff --git a/day3/examples/generic_func2.cc b/chapter_03/examples/generic_func2.cc similarity index 100% rename from day3/examples/generic_func2.cc rename to chapter_03/examples/generic_func2.cc diff --git a/day3/examples/generic_func3.cc b/chapter_03/examples/generic_func3.cc similarity index 100% rename from day3/examples/generic_func3.cc rename to chapter_03/examples/generic_func3.cc diff --git a/day3/examples/generic_func4.cc b/chapter_03/examples/generic_func4.cc similarity index 100% rename from day3/examples/generic_func4.cc rename to chapter_03/examples/generic_func4.cc diff --git a/day3/examples/no_textsub.cc b/chapter_03/examples/no_textsub.cc similarity index 100% rename from day3/examples/no_textsub.cc rename to chapter_03/examples/no_textsub.cc diff --git a/day3/examples/overload_w_concepts.cc b/chapter_03/examples/overload_w_concepts.cc similarity index 100% rename from day3/examples/overload_w_concepts.cc rename to chapter_03/examples/overload_w_concepts.cc diff --git a/chapter_03/examples/print_tuple.cc b/chapter_03/examples/print_tuple.cc new file mode 100644 index 0000000..20378aa --- /dev/null +++ b/chapter_03/examples/print_tuple.cc @@ -0,0 +1,33 @@ +#include <iostream> +#include <string> +#include <tuple> + +template <int idx, int MAX, typename... Args> +struct PRINT_TUPLE { + static void print(std::ostream& strm, const std::tuple<Args...>& t) + { + strm << std::get<idx>(t) << (idx + 1 == MAX ? "" : ", "); + PRINT_TUPLE<idx + 1, MAX, Args...>::print(strm, t); + } +}; + +template <int MAX, typename... Args> +struct PRINT_TUPLE<MAX, MAX, Args...> { + static void print(std::ostream& strm, const std::tuple<Args...>& t) + { + } +}; + +template <typename... Args> +std::ostream& operator<<(std::ostream& strm, const std::tuple<Args...>& t) +{ + strm << "["; + PRINT_TUPLE<0, sizeof...(Args), Args...>::print(strm, t); + return strm << "]"; +} + +//int main() +//{ +// std::tuple<int, std::string, double, int, double > t{23,"abc",3.141, 3, 2.718}; +// std::cout << t << "\n"; +//} diff --git a/chapter_03/examples/print_tuple_cxx17.cc b/chapter_03/examples/print_tuple_cxx17.cc new file mode 100644 index 0000000..d45bfc0 --- /dev/null +++ b/chapter_03/examples/print_tuple_cxx17.cc @@ -0,0 +1,32 @@ +#include <iostream> +#include <string> +#include <tuple> + +template <int idx, int MAX, typename... Args> +struct PRINT_TUPLE { + static void print(std::ostream& strm, const std::tuple<Args...>& t) + { + if + constexpr(idx < MAX) + { + strm << std::get<idx>(t); + if + constexpr((idx + 1) < MAX) strm << ", "; + PRINT_TUPLE<idx + 1, MAX, Args...>::print(strm, t); + } + } +}; + +template <typename... Args> +auto operator<<(std::ostream& strm, const std::tuple<Args...>& t) -> std::ostream& +{ + strm << "["; + PRINT_TUPLE<0, sizeof...(Args), Args...>::print(strm, t); + return strm << "]"; +} + +auto main() -> int +{ + std::tuple<int, std::string, double, int, double> t{ 23, "abc", 3.141, 3, 2.718 }; + std::cout << t << "\n"; +} diff --git a/chapter_03/examples/print_tuple_foldex.cc b/chapter_03/examples/print_tuple_foldex.cc new file mode 100644 index 0000000..2483331 --- /dev/null +++ b/chapter_03/examples/print_tuple_foldex.cc @@ -0,0 +1,31 @@ +#include <tuple> +#include <iostream> +#include <iomanip> + +template <class ... Args> +auto operator<<(std::ostream & strm, const std::tuple<Args...> & t) -> std::ostream & +{ + using namespace std; + auto print_one = [&strm](const auto & onearg) -> ostream & { + using bare_type = remove_cv_t<remove_reference_t<decltype(onearg)>>; + if constexpr (is_same_v<bare_type, string>) + strm << quoted(onearg); + else + strm << onearg; + return strm; + }; + auto print_components = [&](const auto & ... args){ + ((print_one(args) << ", "), ...); + }; + strm << "["; + apply(print_components, t); + return strm <<"]"; +} + +auto main() -> int +{ + std::tuple t1{1, "one"}; + std::tuple t2{2, "two", "II", 2.0}; + std::cout << t1 << "\t" << t2 << "\n"; +} + diff --git a/day3/examples/static_assert0.cc b/chapter_03/examples/static_assert0.cc similarity index 100% rename from day3/examples/static_assert0.cc rename to chapter_03/examples/static_assert0.cc diff --git a/day3/examples/static_assert1.cc b/chapter_03/examples/static_assert1.cc similarity index 100% rename from day3/examples/static_assert1.cc rename to chapter_03/examples/static_assert1.cc diff --git a/day3/examples/static_assert2.cc b/chapter_03/examples/static_assert2.cc similarity index 100% rename from day3/examples/static_assert2.cc rename to chapter_03/examples/static_assert2.cc diff --git a/day3/examples/template_intro.cc b/chapter_03/examples/template_intro.cc similarity index 100% rename from day3/examples/template_intro.cc rename to chapter_03/examples/template_intro.cc diff --git a/chapter_03/examples/variadic_1.cc b/chapter_03/examples/variadic_1.cc new file mode 100644 index 0000000..796578c --- /dev/null +++ b/chapter_03/examples/variadic_1.cc @@ -0,0 +1,39 @@ +#include <iostream> +#include <string> +#if __has_include(<boost/type_index.hpp>) +#include <boost/type_index.hpp> +constexpr auto use_boost_type_index { true }; +#else +#include <typeinfo> +constexpr auto use_boost_type_index { false }; +#endif + + +// plain_type_of(var) returns the typename without reference +// symbols, const qualifiers etc. +// +template <class T> +auto plain_type_of(T&& var) -> std::string +{ + if constexpr (use_boost_type_index) + return boost::typeindex::type_id<T>().pretty_name(); + else + return typeid(var).name(); +} + +template <class... Types> +void f(Types&&... args) +{ + std::cout << "Printing out typenames without references etc. and values\n"; + ((std::cout << plain_type_of(args) << ": " << args << "\n"), ...); +} + +auto main() -> int +{ + const int i { 3 }, j {}; + size_t k {}, l { 9 }; + const char* cst { "C-style string..." }; + std::string cppst { "C++ string..." }; + f(i, j, true, k, l, cst, cppst); +} + diff --git a/chapter_03/examples/variadic_2.cc b/chapter_03/examples/variadic_2.cc new file mode 100644 index 0000000..c388ce9 --- /dev/null +++ b/chapter_03/examples/variadic_2.cc @@ -0,0 +1,27 @@ +#include <iostream> + +template <typename... Types> +void f(Types... args); +template <typename Type1, typename... Types> +void f(Type1 arg1, Types... rest) +{ + std::cout << " The first argument is " << arg1 + << ". Remainder argument list has " << sizeof...(Types) << " elements.\n"; + f(rest...); +} +template <> +void f() {} +template <typename... Types> +void g(Types... args) +{ + std::cout << "Inside g: going to call function f with the sizes of my arguments\n"; + f(sizeof(args)...); +} + +int main() +{ + std::cout << R"hoho(Calling f(0,true," 123 ");)hoho" << '\n'; + f(0, true, "123"); + std::cout << R"hoho(Calling g(0,true," 123 ");)hoho" << '\n'; + g(0, true, "123"); +} diff --git a/chapter_03/examples/variadic_3.cc b/chapter_03/examples/variadic_3.cc new file mode 100644 index 0000000..249c68b --- /dev/null +++ b/chapter_03/examples/variadic_3.cc @@ -0,0 +1,36 @@ +#include <array> +#include <iostream> +#include <list> +#include <numeric> + +template <class... Types> +void increment_all(Types&... args) { (++args, ...); } +template <class... Ts> +void h(Ts... args) +{ + std::cout << "Printing parameters passed to h \n"; + ((std::cout << args << "\t"), ...); + std::cout << "\n"; + [=, &args...] { return increment_all(args...); }(); + + std::cout << "\nModified value due to call to increment_all() through lambda \n"; + ((std::cout << args << "\t"), ...); + std::cout << "\n"; + + std::cout << "Creating std::array out of parameters...\n"; + std::array t { args... }; + + std::cout << "\nsum = " << std::reduce(t.begin(), t.end()) << "\n"; +} +auto main() -> int +{ + int i = 0; + std::list l { 0, 2, 4, 8, 16 }; + auto it = l.begin(); + std::cout << "i = " << i << "; iterator it points to list element of value " << (*it) << "\n"; + std::cout << "increment_all(i, it)\n"; + increment_all(i, it); + std::cout << "i = " << i << "; iterator it points to list element of value " << (*it) << "\n"; + std::cout << "Calling h(1, 2, 3, 4)\n"; + h(1, 2, 3, 4); +} diff --git a/day4/examples/variadic_3b.cc b/chapter_03/examples/variadic_3b.cc similarity index 100% rename from day4/examples/variadic_3b.cc rename to chapter_03/examples/variadic_3b.cc -- GitLab