{"id":17808,"date":"2024-09-02T14:45:13","date_gmt":"2024-09-02T14:45:13","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=17808"},"modified":"2025-08-25T15:16:22","modified_gmt":"2025-08-25T15:16:22","slug":"concepts-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=17808","title":{"rendered":"std::forward with concept (C++)"},"content":{"rendered":"<p><strong>Introduction<\/strong><\/p>\n<ul>\n<li>std::forward: Keeps the type of the reference. (lvalue, rvalue)<\/li>\n<li>concept: Specifies requirements for the template type.<\/li>\n<\/ul>\n<hr>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;concepts&gt;\r\n#include &lt;iostream&gt;\r\n\r\nstruct Functor {\r\n    void operator()() &amp; { std::cout &lt;&lt; &quot;Called on lvalue\\n&quot;; }\r\n    void operator()() &amp;&amp; { std::cout &lt;&lt; &quot;Called on rvalue\\n&quot;; }\r\n};\r\n\r\ntemplate &lt;typename T&gt;\r\nconcept Callable = requires(T t) { { t() }; };\r\n\/\/ The same: &quot;= requires(T&amp;&amp; t) { { std::forward&lt;T&gt;(t)() }; };&quot;\r\n\r\ntemplate &lt;Callable C&gt;\r\nvoid execute(C&amp;&amp; c) { std::forward&lt;C&gt;(c)(); }\r\n\/\/ Calls &quot;operator()() &amp;&quot; : &quot;void execute(C&amp;&amp; c) { c(); }&quot;\r\n\/\/ Calls &quot;operator()() &amp;&amp;&quot;: &quot;void execute(C&amp;&amp; c) { std::move(c)(); }&quot;\r\n\r\nint main() {\r\n    Functor ftor;\r\n    execute(ftor);\r\n    execute(std::move(ftor)); \/\/ std::move makes ftor an rvalue\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<pre>\r\n$ g++ main.cpp -std=c++20\r\n$ .\/a.out\r\nCalled on lvalue\r\nCalled on rvalue\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Introduction std::forward: Keeps the type of the reference. (lvalue, rvalue) concept: Specifies requirements for the template type. Example $ g++<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/17808"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17808"}],"version-history":[{"count":20,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/17808\/revisions"}],"predecessor-version":[{"id":19103,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/17808\/revisions\/19103"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17808"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}