{"id":1091,"date":"2018-10-14T07:31:28","date_gmt":"2018-10-14T02:01:28","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=1091"},"modified":"2018-10-14T07:39:55","modified_gmt":"2018-10-14T02:09:55","slug":"c17-stdapply-and-stdinvoke","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/","title":{"rendered":"C++17 &#8211; std::apply() and std::invoke()"},"content":{"rendered":"<p>Calling a function (or function object) dynamically, through a pointer known at runtime, is a common programming scenario. Almost all languages support this use case.<\/p>\n<p>Lisp, for example, has <em><strong>apply<\/strong><\/em> and <em><strong>funcall<\/strong><\/em>.<\/p>\n<figure id=\"attachment_1092\" aria-describedby=\"caption-attachment-1092\" style=\"width: 360px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Lisp-apply.png?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"1092\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/lisp-apply\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Lisp-apply.png\" data-orig-size=\"324,178\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Apply and Funcall in Lisp\" data-image-description=\"&lt;p&gt;Apply and Funcall in Lisp&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Apply and Funcall in Lisp&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Lisp-apply.png\" class=\"wp-image-1092\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Lisp-apply.png?resize=360%2C198&#038;ssl=1\" alt=\"Apply and Funcall in Lisp\" width=\"360\" height=\"198\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Lisp-apply.png?w=324&amp;ssl=1 324w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Lisp-apply.png?resize=300%2C165&amp;ssl=1 300w\" sizes=\"(max-width: 360px) 100vw, 360px\" \/><\/a><figcaption id=\"caption-attachment-1092\" class=\"wp-caption-text\"><strong>Apply and Funcall in Lisp<\/strong><\/figcaption><\/figure>\n<p>When using <em><strong>apply<\/strong><\/em>, you can see that the arguments are passed via a separate <em><strong>list<\/strong><\/em> object. With <em><strong>funcall<\/strong><\/em>, however, the arguments are passed directly in line.<\/p>\n<p>Likewise, <em><strong>Mathematica<\/strong><\/em> has <em><strong>Apply<\/strong><\/em> and <em><strong>Construct<\/strong><\/em>:<\/p>\n<figure id=\"attachment_1093\" aria-describedby=\"caption-attachment-1093\" style=\"width: 354px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Mathematica.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1093\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/mathematica\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Mathematica.png\" data-orig-size=\"354,272\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Apply and Construct in Mathematica\" data-image-description=\"&lt;p&gt;Apply and Construct in Mathematica&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Apply and Construct in Mathematica&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Mathematica.png\" class=\"size-full wp-image-1093\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Mathematica.png?resize=354%2C272&#038;ssl=1\" alt=\"Apply and Construct in Mathematica\" width=\"354\" height=\"272\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Mathematica.png?w=354&amp;ssl=1 354w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Mathematica.png?resize=300%2C231&amp;ssl=1 300w\" sizes=\"(max-width: 354px) 100vw, 354px\" \/><\/a><figcaption id=\"caption-attachment-1093\" class=\"wp-caption-text\"><strong>Apply and Construct in Mathematica<\/strong><\/figcaption><\/figure>\n<p>As you can see, the idea is quite similar to <em><strong>Lisp<\/strong><\/em>.<\/p>\n<p>Even in C++ (prior to <strong>C++17<\/strong>), it has always been possible to call functions and methods dynamically through respective pointers. Take a look at the following example.<\/p>\n<figure id=\"attachment_1094\" aria-describedby=\"caption-attachment-1094\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Pointers.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1094\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/pointers\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Pointers.png\" data-orig-size=\"536,371\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Pointers in C++\" data-image-description=\"&lt;p&gt;Pointers in C++&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Pointers in C++&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Pointers.png\" class=\"wp-image-1094\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Pointers.png?resize=650%2C450&#038;ssl=1\" alt=\"Pointers in C++\" width=\"650\" height=\"450\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Pointers.png?w=536&amp;ssl=1 536w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Pointers.png?resize=300%2C208&amp;ssl=1 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1094\" class=\"wp-caption-text\"><strong>Pointers in C++<\/strong><\/figcaption><\/figure>\n<p>What is nice about <strong>C++17<\/strong> is that it provides two methods <em><strong>apply<\/strong><\/em> and <em><strong>invoke<\/strong><\/em> (defined in the <em><strong>std<\/strong><\/em> namespace) that make dynamic method invocation more flexible and uniform. You will see that the behavior is quite similar to <em><strong>Lisp<\/strong><\/em> and <em><strong>Mathematica<\/strong><\/em>.<\/p>\n<p>To make use of <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/apply\" target=\"_blank\" rel=\"noopener\"><em><strong>std::apply<\/strong><\/em><\/a>\u00a0and <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/functional\/invoke\" target=\"_blank\" rel=\"noopener\"><em><strong>std:: invoke<\/strong><\/em><\/a>\u00a0you have to <em><strong>#include &lt;functional&gt;<\/strong><\/em><\/p>\n<p>Let us define a simple function that takes 3 integers and returns their sum. Let us also define a class (actually a struct) that has a public member function and an instance variable.<\/p>\n<figure id=\"attachment_1095\" aria-describedby=\"caption-attachment-1095\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code1.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1095\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/code1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code1.png\" data-orig-size=\"402,340\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"A Simple Function and A Class\" data-image-description=\"&lt;p&gt;A Simple Function and A Class&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;A Simple Function and A Class&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code1.png\" class=\"wp-image-1095\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code1.png?resize=500%2C423&#038;ssl=1\" alt=\"A Simple Function and A Class\" width=\"500\" height=\"423\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code1.png?w=402&amp;ssl=1 402w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code1.png?resize=300%2C254&amp;ssl=1 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-1095\" class=\"wp-caption-text\"><strong>A Simple Function and A Class<\/strong><\/figcaption><\/figure>\n<p>The <em><strong>apply<\/strong><\/em> method takes two arguments namely, the <em><strong>callable<\/strong><\/em> object that needs to be dynamically invoked, and the data structure that contains the <em><strong>arguments<\/strong><\/em> for the callable object. The data structure could be <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/pair\" target=\"_blank\" rel=\"noopener\"><em><strong>std::pair<\/strong><\/em><\/a>, <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/tuple\"><em><strong>std::tuple<\/strong><\/em><\/a> or <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/array\"><em><strong>std::array<\/strong><\/em><\/a>.<\/p>\n<p>So let us create a tuple and array of 3 integers and see how to use <em><strong>apply<\/strong><\/em><\/p>\n<figure id=\"attachment_1096\" aria-describedby=\"caption-attachment-1096\" style=\"width: 651px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code2.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1096\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/code2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code2.png\" data-orig-size=\"745,356\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Calling std::apply\" data-image-description=\"&lt;p&gt;Calling std::apply&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Calling std::apply&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code2.png\" class=\"wp-image-1096\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code2.png?resize=651%2C311&#038;ssl=1\" alt=\"Calling std::apply\" width=\"651\" height=\"311\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code2.png?w=745&amp;ssl=1 745w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code2.png?resize=300%2C143&amp;ssl=1 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/a><figcaption id=\"caption-attachment-1096\" class=\"wp-caption-text\"><strong>Calling std::apply<\/strong><\/figcaption><\/figure>\n<p>Notice that we cannot pass the original arguments directly in line when calling <em><strong>apply<\/strong><\/em>.<\/p>\n<p>This is the difference between <em><strong>apply<\/strong><\/em> and <em><strong>invoke<\/strong><\/em>. <em><strong>invoke<\/strong><\/em> requires the arguments to be passed in line as if we are calling the function directly.<\/p>\n<figure id=\"attachment_1097\" aria-describedby=\"caption-attachment-1097\" style=\"width: 653px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code3.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1097\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/code3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code3.png\" data-orig-size=\"709,114\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Calling std::invoke\" data-image-description=\"&lt;p&gt;Calling std::invoke&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Calling std::invoke&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code3.png\" class=\"wp-image-1097\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code3.png?resize=653%2C105&#038;ssl=1\" alt=\"Calling std::invoke\" width=\"653\" height=\"105\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code3.png?w=709&amp;ssl=1 709w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code3.png?resize=300%2C48&amp;ssl=1 300w\" sizes=\"(max-width: 653px) 100vw, 653px\" \/><\/a><figcaption id=\"caption-attachment-1097\" class=\"wp-caption-text\"><strong>Calling std::invoke<\/strong><\/figcaption><\/figure>\n<p><em><strong>apply<\/strong><\/em> comes in quite handy if we are calling a <em><strong>varargs<\/strong><\/em> function such as <em><strong>printf<\/strong><\/em>. We can conveniently bundle all the arguments in a <em><strong>tuple<\/strong><\/em> and pass it to <em><strong>apply<\/strong><\/em>.<\/p>\n<figure id=\"attachment_1098\" aria-describedby=\"caption-attachment-1098\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code4.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1098\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/code4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code4.png\" data-orig-size=\"625,132\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Calling a Varargs Functions\" data-image-description=\"&lt;p&gt;Calling a Varargs Functions&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Calling a Varargs Functions&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code4.png\" class=\"wp-image-1098\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code4.png?resize=650%2C137&#038;ssl=1\" alt=\"Calling a Varargs Functions\" width=\"650\" height=\"137\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code4.png?w=625&amp;ssl=1 625w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code4.png?resize=300%2C63&amp;ssl=1 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1098\" class=\"wp-caption-text\"><strong>Calling a Varargs Function<\/strong><\/figcaption><\/figure>\n<p>Let us consider the case where instead of calling normal functions, we wish to invoke <em><strong>member<\/strong> <strong>functions<\/strong><\/em>. Remember that in order to invoke a member function, we need an <em><strong>object recipient<\/strong><\/em> (unless the function is static).<\/p>\n<p>Both <em><strong>apply<\/strong><\/em> and <em><strong>invoke<\/strong><\/em> make this process quite simple and uniform.<\/p>\n<figure id=\"attachment_1099\" aria-describedby=\"caption-attachment-1099\" style=\"width: 649px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1099\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/code5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png\" data-orig-size=\"735,307\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Calling Member Functions\" data-image-description=\"&lt;p&gt;Calling Member Functions&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Calling Member Functions&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png\" class=\"wp-image-1099\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=649%2C271&#038;ssl=1\" alt=\"Calling Member Functions\" width=\"649\" height=\"271\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?w=735&amp;ssl=1 735w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=300%2C125&amp;ssl=1 300w\" sizes=\"(max-width: 649px) 100vw, 649px\" \/><\/a><figcaption id=\"caption-attachment-1099\" class=\"wp-caption-text\"><strong>Calling Member Functions<\/strong><\/figcaption><\/figure>\n<p>Compare this with the traditional call syntax (shown within comments above). Hope you are able to appreciate the benefits of <em><strong>apply<\/strong><\/em> and <em><strong>invoke<\/strong><\/em>.<\/p>\n<p>Implementations of these two functions depend on some fancy template coding tricks, but fortunately as users, we do not need to know the details.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>You can download my example source code <a href=\"http:\/\/www.rangakrish.com\/downloads\/ApplyAndInvoke.cpp\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a great weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calling a function (or function object) dynamically, through a pointer known at runtime, is a common programming scenario. Almost all languages support this use case. Lisp, for example, has apply and funcall. When using apply, you can see that the arguments are passed via a separate list object. With funcall, however, the arguments are passed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[49,17],"tags":[118,160,161],"class_list":["post-1091","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c17","tag-stdapply","tag-stdinvoke"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-hB","jetpack-related-posts":[{"id":122,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/01\/06\/lisp-returning-multiple-values-from-a-function\/","url_meta":{"origin":1091,"position":0},"title":"Lisp: Returning Multiple Values From A Function","author":"admin","date":"January 6, 2016","format":false,"excerpt":"One of the interesting features of Lisp is its support for returning multiple values from a function, without bundling the values in a special container. Languages such as Python and Ruby support multi-value return (although there are subtle differences from Lisp). In C++11, we can use std::make_tuple() and std::tie(). To\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":558,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/07\/07\/clpython-python-in-common-lisp\/","url_meta":{"origin":1091,"position":1},"title":"CLPython &#8211; Python in Common Lisp","author":"admin","date":"July 7, 2017","format":false,"excerpt":"My work in the area of NLP requires\u00a0me to work with several frameworks across multiple languages such as Java, Python and Lisp. Sometime ago I got a chance to experiment with CLPython, an open-source implementation of Python in Common Lisp. Although CLPython is not under active development now, I found\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2975,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/12\/25\/stdtie\/","url_meta":{"origin":1091,"position":2},"title":"Std::tie","author":"admin","date":"December 25, 2022","format":false,"excerpt":"std::tuple is a widely used abstraction in C++ and has been around since C++11. It is a generalization of std::pair. std::tie is convenient when we want to create a tuple of lvalue references to existing variables. It is a function template commonly used to unpack a tuple into individual objects.\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"std::tie Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1928,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","url_meta":{"origin":1091,"position":3},"title":"std::is_destructible","author":"admin","date":"March 15, 2020","format":false,"excerpt":"In the last article, I explained the deleted destructor in some detail. Today, I would like to talk about a related construct, a type trait called std::is_destructible. Type traits, defined in the header <type_traits>, are a big help when it comes to implementing template metaprogramming. \u00a0See this\u00a0article for a nice\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Private\/Protected Destructor","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1278,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/12\/02\/book-review-functional-programming-in-c\/","url_meta":{"origin":1091,"position":4},"title":"Book Review: Functional Programming in C++","author":"admin","date":"December 2, 2018","format":false,"excerpt":"Title: Functional Programming in C++: How to improve your C++ programs using functional techniques Author: Ivan Cukic Publisher: Manning Publications Co. Year: 2019 This book came out recently, about a week ago. I bought it immediately, and when I started to read it, I couldn\u2019t put it down! Very informative\u2026","rel":"","context":"In &quot;Book Review&quot;","block_context":{"text":"Book Review","link":"https:\/\/www.rangakrish.com\/index.php\/category\/book-review\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":47,"url":"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/13\/multimethods-in-lisp\/","url_meta":{"origin":1091,"position":5},"title":"Multimethods in Lisp","author":"admin","date":"October 13, 2015","format":false,"excerpt":"In object-oriented languages such as C++ and Java, virtual functions are dynamically dispatched based on the runtime type of the receiving object. But if such virtual functions have an argument that is itself based on a class hierarchy, there is no way to associate dynamic dispatch based on both the\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Multimethods Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=1091"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1091\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=1091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=1091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=1091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}