{"id":4994,"date":"2022-02-16T11:21:40","date_gmt":"2022-02-16T10:21:40","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=4994"},"modified":"2022-02-16T11:50:55","modified_gmt":"2022-02-16T10:50:55","slug":"c-guns-play-with-stdtuple-and-stdapply","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=4994","title":{"rendered":"C++ Guns: Play with std::tuple and std::apply"},"content":{"rendered":"<p><a href=\"http:\/\/roboblog.fatal-fury.de\/?p=3474\">Part 1: print std::array with std::integer_sequence<\/a><br \/>\n<a href=\"http:\/\/roboblog.fatal-fury.de\/?p=3508\">Part 2: convert tuple to parameter pack<\/a><br \/>\n<a href=\"http:\/\/roboblog.fatal-fury.de\/?p=3514\">Part 3: print std::array with std::apply and fold<\/a><br \/>\n<a href=\"http:\/\/roboblog.fatal-fury.de\/?p=3529\">Part 4: fold over std::tuple und erzeugten Assembler Code<\/a><br \/>\n<a href=\"http:\/\/roboblog.fatal-fury.de\/?p=3538\">Part 5: fold over std::tuple of std::vector of Types ...<\/a><br \/>\n<a href=\"http:\/\/roboblog.fatal-fury.de\/?p=3617\">Part 6: apply generic lambda to tuple<\/a><br \/>\n<strong>Part 7: Play with std::tuple and std::apply<\/strong><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;tuple&gt;\r\n#include &lt;string_view&gt;\r\n#include &lt;iostream&gt;\r\n\r\nvoid print(int i, std::string_view str, double x) {\r\n    std::cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; str &lt;&lt; &quot; &quot; &lt;&lt; x &lt;&lt; &quot;\\n&quot;;\r\n}\r\n\r\n\r\n\r\nint main() {\r\n    std::tuple tp {1, &quot;abc&quot;, 3.1};\r\n\r\n    \/\/ Ein Tuple in seine Einzelteile zerlegten durch eine Funktion die genau\r\n    \/\/ die Tuple Elemente als Parameter hat\r\n    std::apply(print, tp);\r\n\r\n\r\n    \/\/ Ein Tuple in sein erstes Element und den Rest zerlergen.\r\n    \/\/ Der Rest ist ne variadic argument list\r\n    \/\/ Die Funktion print2 ist absichtlich ein Lambda, \r\n    \/\/ da das sonst mit dem auto Argument Typ nicht funktioniert. \r\n    \/\/ Will man print2 als freie Funktion haben, muss man sie als template schreiben.\r\n    auto print2 = &#x5B;](const int i, const auto&amp;... restArgs) {\r\n        std::cout &lt;&lt; i &lt;&lt; &quot; Anzahl Restargumente: &quot; &lt;&lt; sizeof...(restArgs) &lt;&lt; &quot;\\n&quot;;\r\n    };\r\n    std::apply(print2, tp);\r\n}\r\n<\/pre>\n<blockquote><p>1 abc 3.1<br \/>\n1 Anzahl Restargumente: 2<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Part 1: print std::array with std::integer_sequence Part 2: convert tuple to parameter pack Part 3: print std::array with std::apply and fold Part 4: fold over std::tuple und erzeugten Assembler Code Part 5: fold over std::tuple of std::vector of Types ... Part 6: apply generic lambda to tuple Part 7: Play with std::tuple and std::apply #include [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-4994","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/4994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4994"}],"version-history":[{"count":3,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/4994\/revisions"}],"predecessor-version":[{"id":5004,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/4994\/revisions\/5004"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4994"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}