{"id":198,"date":"2016-04-08T15:22:29","date_gmt":"2016-04-08T15:22:29","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=198"},"modified":"2016-04-11T01:50:07","modified_gmt":"2016-04-11T01:50:07","slug":"c14-return-type-deduction-for-normal-functions","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2016\/04\/08\/c14-return-type-deduction-for-normal-functions\/","title":{"rendered":"C++14: Return Type Deduction for Normal Functions"},"content":{"rendered":"<p>In C++11, a function must specify its return type in one of the following two ways:<\/p>\n<p style=\"padding-left: 30px;\">int aFunction(int arg) {<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return arg * 2;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 90px;\"><strong>(or)<\/strong><\/p>\n<p style=\"padding-left: 30px;\">auto aFunction(int arg) -&gt; int {<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return arg * 2;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>The first is the classic function definition syntax. The second form was introduced in C++11, and is useful if, for example, the return type depends on the argument types. This situation arises sometimes in the case of template functions:<\/p>\n<p style=\"padding-left: 30px;\">template&lt;typename T1, typename T2&gt;<\/p>\n<p style=\"padding-left: 30px;\">auto aTemplateFunction(T1 arg1, T1 arg2, T2 arg3) -&gt; decltype(arg2 * arg3) {<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return arg2 * (arg3 &#8211; 1);<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>Here the <em><strong>decltype<\/strong><\/em> specifier comes in handy to determine the type of the return expression.<\/p>\n<p>In C++14, the compiler is supposed to deduce the return type automatically, so the following is acceptable:<\/p>\n<p style=\"padding-left: 30px;\">auto aFunction(int arg) {<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return arg * 2;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>and even this:<\/p>\n<p style=\"padding-left: 30px;\">template&lt;typename T1, typename T2&gt;<\/p>\n<p style=\"padding-left: 30px;\">auto aTemplateFunction(T1 arg1, T1 arg2, T2 arg3)\u00a0 {<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return arg2 * arg3 + 8;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>The requirement is that the return value must be of the <em><strong>same type<\/strong><\/em> along <em><strong>every<\/strong><\/em> return path in the function body.<\/p>\n<p>The following will trigger compilation error:<\/p>\n<p style=\"padding-left: 30px;\">auto badFunction(int arg) {<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 if (arg &gt; 0)<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 \u00a0 \u00a0 return arg; \/\/ \u2018int\u2019 return type<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return arg * 1.2; \/\/ \u2018double\u2019<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>This automatic return type deduction will work for recursive functions too, as long as there is a <em><strong>return<\/strong><\/em> preceding the recursive call.<\/p>\n<p style=\"padding-left: 30px;\">\/\/ Precondition: N &gt;= 0<\/p>\n<p style=\"padding-left: 30px;\">auto factorial(int N)<\/p>\n<p style=\"padding-left: 30px;\">{<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 if(N == 0 || N == 1)<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 \u00a0 \u00a0 return 1;<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return N * factorial (N &#8211; 1);<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>The above works as expected, but the following is not valid:<\/p>\n<p style=\"padding-left: 30px;\">\/\/ Precondition: N &gt;= 0<\/p>\n<p style=\"padding-left: 30px;\">auto factorial2(int N)<\/p>\n<p style=\"padding-left: 30px;\">{<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 if(N &gt; 1)<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 \u00a0 \u00a0 return N * factorial2 (N &#8211; 1); \/\/ No previous return to deduce type<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return 1;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>This automatic return type deduction feature is quite useful for the following reasons:<\/p>\n<p>1) In many\u00a0cases, we\u00a0will have to type less (compared to the actual return type)!<\/p>\n<p>2) During development phase, we tend to refactor the code, possibly resulting in different return types at different times. With <em><strong>auto<\/strong><\/em>, we need not worry about keeping the return type declaration in sync with what is actually returned. The compiler does it for us.<\/p>\n<p>3) Some return types can be pretty verbose (without proper typedef). Using <em><strong>auto<\/strong><\/em> promotes readability in such cases. A simple example is pointer to functions.<\/p>\n<p>4) What if the returned value is a lambda, or worse still, a function that returns a lambda (and so on\u2026)? Let the compiler figure it out!<\/p>\n<p style=\"padding-left: 30px;\">auto getLambda(int delta) {<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return [=](int arg) {return arg + delta;};<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">auto getFunctionThatReturnsLambda() {<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return getLambda;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>Compare the above to what we\u00a0would have to do in case <em><strong>auto<\/strong><\/em> was not allowed for the return type:<\/p>\n<p style=\"padding-left: 30px;\"><strong>std::function&lt;int(int)&gt;<\/strong><\/p>\n<p style=\"padding-left: 30px;\">getLambda(int delta) {<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return [=](int arg) {return arg + delta;};<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\"><strong>std::function&lt;std::function&lt;int(int)&gt;(int)&gt;<\/strong><\/p>\n<p style=\"padding-left: 30px;\">getFunctionThatReturnsLambda() {<\/p>\n<p style=\"padding-left: 30px;\">\u00a0 \u00a0 return getLambda;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>Here we use the <em><strong>std::function&lt;&gt;<\/strong><\/em> template defined in <em><strong>&lt;functional&gt;<\/strong><\/em> STL header.\u00a0Obviously, <em><strong>auto<\/strong><\/em> makes things much easier.<\/p>\n<p>Overall, the <em><strong>auto<\/strong><\/em>\u00a0return type specification\u00a0introduced in C++14 is a very useful feature, something that every C++ programmer would use everyday. With <em><strong>Visual Studio 2015 Update 2<\/strong><\/em> adding support for C++14, Windows programmers will be a happy lot!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C++11, a function must specify its return type in one of the following two ways: int aFunction(int arg) { \u00a0 \u00a0 return arg * 2; } (or) auto aFunction(int arg) -&gt; int { \u00a0 \u00a0 return arg * 2; } The first is the classic function definition syntax. The second form was introduced in [&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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[49,17],"tags":[50,51],"class_list":["post-198","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c14","tag-return-type-deduction"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-3c","jetpack-related-posts":[{"id":1973,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/","url_meta":{"origin":198,"position":0},"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":928,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/05\/27\/calling-go-functions-from-lisp\/","url_meta":{"origin":198,"position":1},"title":"Calling Go Functions from Lisp","author":"admin","date":"May 27, 2018","format":false,"excerpt":"For the past few weeks I was travelling in the USA and had the pleasure of meeting several friends, some of whom are senior developers and architects. It was interesting to learn that many of them were either using Go language in their daily work, or were passionately endorsing the\u2026","rel":"","context":"In &quot;Golang&quot;","block_context":{"text":"Golang","link":"https:\/\/www.rangakrish.com\/index.php\/category\/golang\/"},"img":{"alt_text":"Part of Header File","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=525%2C300 1.5x"},"classes":[]},{"id":944,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/06\/10\/calling-lisp-functions-from-go-language\/","url_meta":{"origin":198,"position":2},"title":"Calling Lisp Functions from Go Language","author":"admin","date":"June 10, 2018","format":false,"excerpt":"In the previous post, we saw how to invoke a Go function from Lisp. In today's post, I would like to explain how to go in the other direction, namely, how to call Lisp functins from Go. There are two ways to do this. The first approach is to load\u2026","rel":"","context":"In &quot;Golang&quot;","block_context":{"text":"Golang","link":"https:\/\/www.rangakrish.com\/index.php\/category\/golang\/"},"img":{"alt_text":"Direct DLL Call","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/06\/LispCall.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/06\/LispCall.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/06\/LispCall.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2280,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/","url_meta":{"origin":198,"position":3},"title":"C++20 [[nodiscard]] Attribute","author":"admin","date":"January 17, 2021","format":false,"excerpt":"[[nodiscard]]\u00a0attribute \u201cencourages\u201d the compiler to issue a warning when the return value from a function is ignored. It was introduced in C++ 17 and enhanced in C++ 20 to include a string literal that can be used as an explanation of the warning. Let us look at different cases one\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Case-1: Enumeration","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1960,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/","url_meta":{"origin":198,"position":4},"title":"std::common_type Type Trait","author":"admin","date":"April 12, 2020","format":false,"excerpt":"In today's post, I would like to go over the type trait std::common_type<>. This trait was introduced in C++11. As per the specification, std::common_type<T1, T2, ...Tn>::type refers to a type Tx in the given list, which the rest of the types in the list can be implicitly converted to. This\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Program Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig5.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":558,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/07\/07\/clpython-python-in-common-lisp\/","url_meta":{"origin":198,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/198","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=198"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/198\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}