{"id":2975,"date":"2022-12-25T11:28:03","date_gmt":"2022-12-25T05:58:03","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2975"},"modified":"2022-12-25T11:28:03","modified_gmt":"2022-12-25T05:58:03","slug":"stdtie","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/12\/25\/stdtie\/","title":{"rendered":"Std::tie"},"content":{"rendered":"<p><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/tuple\" target=\"_blank\" rel=\"noopener\"><em><strong>std::tuple<\/strong><\/em><\/a> is a widely used abstraction in <em><strong>C++<\/strong><\/em> and has been around since <em><strong>C++11<\/strong><\/em>. It is a generalization of <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/pair\" target=\"_blank\" rel=\"noopener\"><em><strong>std::pair<\/strong><\/em><\/a>.<\/p>\n<p><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/tuple\/tie\" target=\"_blank\" rel=\"noopener\"><em><strong>std::tie<\/strong><\/em><\/a> is convenient when we want to create a tuple of <strong><em>lvalue references<\/em><\/strong> to existing variables. It is a function template commonly used to unpack a tuple into individual objects.<\/p>\n<figure id=\"attachment_2976\" aria-describedby=\"caption-attachment-2976\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2976\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/12\/25\/stdtie\/example1-29\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1.jpg\" data-orig-size=\"861,408\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1671874154&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=\"std::tie Example\" data-image-description=\"&lt;p&gt;std::tie Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;std::tie Example&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1.jpg\" class=\"wp-image-2976\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1.jpg?resize=550%2C261&#038;ssl=1\" alt=\"std::tie Example\" width=\"550\" height=\"261\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1.jpg?resize=300%2C142&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1.jpg?resize=768%2C364&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1.jpg?w=861&amp;ssl=1 861w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2976\" class=\"wp-caption-text\"><strong>std::tie Example<\/strong><\/figcaption><\/figure>\n<p>In the above example, we first define a tuple object comprising an integer, a string and a double. We then use <em><strong>std::tie<\/strong><\/em> to assign the respective values of the tuple to the individual variables <em><strong>\u201cI\u201d<\/strong><\/em> and <em><strong>\u201cd\u201d<\/strong><\/em>. This works because, as mentioned earlier, <em><strong>std::tie<\/strong><\/em> returns a tuple with lvalue references to its parameters. Notice the use of <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/tuple\/ignore\" target=\"_blank\" rel=\"noopener\"><em><strong>std::ignore<\/strong><\/em><\/a> to skip an element of the RHS tuple.<\/p>\n<p>The main difference between <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/structured_binding\" target=\"_blank\" rel=\"noopener\"><em><strong>structured binding<\/strong><\/em><\/a> and <em><strong>std::tie<\/strong><\/em> is that the former both <em><strong>creates <\/strong><\/em>and<em><strong> initializes<\/strong><\/em> at the same time, whereas the latter only <em><strong>assigns<\/strong><\/em> values to already existing variables. The following example shows this.<\/p>\n<figure id=\"attachment_2978\" aria-describedby=\"caption-attachment-2978\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2978\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/12\/25\/stdtie\/example2-25\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example2.jpg\" data-orig-size=\"937,280\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1671874233&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=\"Compared with Structured Binding\" data-image-description=\"&lt;p&gt;Compared with Structured Binding&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Compared with Structured Binding&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example2-300x90.jpg\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example2.jpg\" class=\"wp-image-2978\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example2.jpg?resize=550%2C164&#038;ssl=1\" alt=\"Compared with Structured Binding\" width=\"550\" height=\"164\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example2.jpg?resize=300%2C90&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example2.jpg?resize=768%2C229&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example2.jpg?w=937&amp;ssl=1 937w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2978\" class=\"wp-caption-text\"><strong>Compared with Structured Binding<\/strong><\/figcaption><\/figure>\n<p>The last point to keep in mind about <em><strong>std::tie<\/strong><\/em> is that it requires <em><strong>modifiable lvalue<\/strong><\/em> parameters when it is used to <em><strong>\u201cunpack\u201d<\/strong><\/em> a tuple. Take a look this code:<\/p>\n<figure id=\"attachment_2979\" aria-describedby=\"caption-attachment-2979\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example3.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2979\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/12\/25\/stdtie\/example3-21\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example3.jpg\" data-orig-size=\"901,512\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1671959847&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=\"Parameter Requirements\" data-image-description=\"&lt;p&gt;Parameter Requirements&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Parameter Requirements&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example3-300x170.jpg\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example3.jpg\" class=\"wp-image-2979\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example3.jpg?resize=550%2C313&#038;ssl=1\" alt=\"Parameter Requirements\" width=\"550\" height=\"313\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example3.jpg?resize=300%2C170&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example3.jpg?resize=768%2C436&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example3.jpg?w=901&amp;ssl=1 901w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2979\" class=\"wp-caption-text\"><strong>Parameter Requirements<\/strong><\/figcaption><\/figure>\n<p>The reason why <em><strong>line 35<\/strong><\/em> won\u2019t compile is because <em><strong>std::tie<\/strong><\/em> is used on the LHS of assignment to unpack a tuple, but its parameters are <em><strong>literals<\/strong><\/em>. In <em><strong>line 39<\/strong><\/em>, even though the parameter to <em><strong>std::tie<\/strong><\/em> is an lvalue, it is a <em><strong>const<\/strong><\/em> object and hence cannot be modified by the unpacking assignment.<\/p>\n<p><em><strong>Line 43<\/strong><\/em> is OK because here <em><strong>std::tie<\/strong><\/em> is used on the RHS and there is no modification to its argument. <em><strong>Line 48<\/strong><\/em> shows that we can use the <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/tuple\/get\" target=\"_blank\" rel=\"noopener\"><em><strong>std::get<\/strong><\/em><\/a> function to access an individual element of the tuple returned by <em><strong>std::tie<\/strong> <\/em>and update it with a new value.<\/p>\n<p>You can download the sample code <a href=\"http:\/\/www.rangakrish.com\/downloads\/std_tie.cpp\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p><em><strong>Merry Christmas and Happy New Year 2023!<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. In the above example, we [&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_post_was_ever_published":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}},"categories":[49,17],"tags":[67,345,346,344],"class_list":["post-2975","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c","tag-stdignore","tag-stdtie","tag-stdtuple"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s9OLnF-stdtie","jetpack-related-posts":[{"id":1091,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/","url_meta":{"origin":2975,"position":0},"title":"C++17 &#8211; std::apply() and std::invoke()","author":"admin","date":"October 14, 2018","format":false,"excerpt":"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,\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Calling Member Functions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":700,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/","url_meta":{"origin":2975,"position":1},"title":"C++17: std::optional","author":"admin","date":"November 21, 2017","format":false,"excerpt":"Suppose we want to write a function that returns a value, but with the possibility that\u00a0 the computation might fail. This failure can be represented as an exception, or as a return value that unamibiguously denotes failure (for example, -1). Throwing an exception is a strong form of failure and\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2889,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/","url_meta":{"origin":2975,"position":2},"title":"std::array vs. std::vector","author":"admin","date":"August 9, 2022","format":false,"excerpt":"One of the common questions when starting to study the container abstractions in C++20 is \"Should I use std::array\u00a0or std::vector?\". The correct answer is \"use what is best suited for the current situation\". That is not good enough, obviously. In this short article, I will try to answer this question\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"std::array Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.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":2975,"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":1973,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/","url_meta":{"origin":2975,"position":4},"title":"std::is_base_of","author":"admin","date":"April 26, 2020","format":false,"excerpt":"In our on-going series on C++ Type Traits, today's topic is about the trait std::is_base_of<>. For the official description, see this. std::is_base_of<A, B>::value takes two arguments, both classes (or structs), and returns true if A is a base class of B and false otherwise. Trivially, std::<A, A>::value is true. Let\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3106,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/","url_meta":{"origin":2975,"position":5},"title":"std::expected","author":"admin","date":"May 13, 2023","format":false,"excerpt":"I had written about std::optional<> in an earlier article. C++23 introduces std::expected<> as an interesting extension to std::optional<>. Whereas std::optional<> contains a value or none at all, std::expected<> contains a value or an error code associated with it. This gives better control to the caller in terms of handling the\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"An Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1-300x265.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1-300x265.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1-300x265.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2975","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=2975"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2975\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}