{"id":6239,"date":"2021-10-21T10:58:11","date_gmt":"2021-10-21T10:58:11","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/automatic-return-type-c-11-14-20\/"},"modified":"2023-06-26T09:24:26","modified_gmt":"2023-06-26T09:24:26","slug":"automatic-return-type-c-11-14-20","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/automatic-return-type-c-11-14-20\/","title":{"rendered":"Automatic Return Type (C++11\/14\/20)"},"content":{"rendered":"<p>I started my discussion about the&nbsp; &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/automatic-return-type\">Automatic Return Type (C++98)<\/a>&#8221; in my last post. Today, I&#8217;m faced with the same challenge but solve it with C++11, C++14, and C++20.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6233\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/AutomaticReturnType.png\" alt=\"AutomaticReturnType\" width=\"650\" height=\"398\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/AutomaticReturnType.png 914w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/AutomaticReturnType-300x184.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/AutomaticReturnType-768x471.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>To remind you: Here is the challenge I want to solve.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T2<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #555555;\">???<\/span> sum(T t, T2 t2) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> t <span style=\"color: #555555;\">+<\/span> t2;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When you have a function template <code><\/code>with at least two type parameters, you can not decide in general the return type of the function. Of course, <code>sum&nbsp;<\/code>should return the type the arithmetic operation<code> t + t2 <\/code>returns.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(<span style=\"color: #ff6600;\">5.5<\/span> <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">5.5<\/span>).name();    <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(<span style=\"color: #ff6600;\">5.5<\/span> <span style=\"color: #555555;\">+<\/span> <span style=\"color: #336666;\">true<\/span>).name();   <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(<span style=\"color: #336666;\">true<\/span> <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">5.5<\/span>).name();   <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(<span style=\"color: #336666;\">true<\/span> <span style=\"color: #555555;\">+<\/span> <span style=\"color: #336666;\">false<\/span>).name(); <span style=\"color: #0099ff; font-style: italic;\">\/\/ int<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When you want to read the full story, read my previous post &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/automatic-return-type\">Automatic Return Type (C++98)<\/a>&#8220;. Now, I jump to C++11.<\/p>\n<h2>C++11<\/h2>\n<p>In C++11, there are essentially two ways to solve this issue: type-traits or <code>auto<\/code> combination with <code>decltype<\/code>.<\/p>\n<h3>Type-Traits<\/h3>\n<p>The Type-traits library has the function <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/common_type\"><code>std::common_type<\/code><\/a>. This function determines at compile time the common type of an arbitrary number of types. The common type is that type among all types to which all types can be implicitly converted. If this type is not available, you get a compile-time error.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ automaticReturnTypeTypeTraits.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;typeinfo&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;type_traits&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T2<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">typename<\/span> std<span style=\"color: #555555;\">::<\/span>common_type<span style=\"color: #555555;\">&lt;<\/span>T, T2<span style=\"color: #555555;\">&gt;::<\/span>type sum(T t, T2 t2) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> t <span style=\"color: #555555;\">+<\/span> t2;\r\n}\r\n\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #ff6600;\">5.5<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;     <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #ff6600;\">5.5<\/span>, <span style=\"color: #336666;\">true<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #336666;\">true<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #336666;\">true<\/span>, <span style=\"color: #336666;\">false<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ bool<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>For simplicity reasons, I display the string representation of the type in the source code. I used the MSVC compiler. The GCC or Clang compiler would return single characters such as d for <code>double<\/code> and b for bool.<\/p>\n<p>There is one subtle difference between<code> std::common_type<\/code> and all other variants I presented in the last post and this post: <code>std::common_type<\/code> returns the common type, but my traits solution in the last post, &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/automatic-return-type\">Automatic Return Type (C++98)<\/a>&#8220;, and the solutions based on <code>auto <\/code>in this post returns the type to which the expression<code> t + t2<\/code> evaluates to.<\/p>\n<h3><code>auto<\/code> in Combination with <code>decltype<\/code><\/h3>\n<p>Using <code>auto<\/code> to deduce the return type of a function in C++11 is way too verbose.<\/p>\n<p>First, you have to use the so-called trailing return type, and second, you have to specify the return type in a <code>decltype<\/code> expression.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ automaticReturnTypeTypeAutoDecltype.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;typeinfo&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;type_traits&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T2<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> sum(T t, T2 t2) <span style=\"color: #555555;\">-&gt;<\/span> decltype(t <span style=\"color: #555555;\">+<\/span> t2) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> t <span style=\"color: #555555;\">+<\/span> t2;\r\n}\r\n\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #ff6600;\">5.5<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;     <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #ff6600;\">5.5<\/span>, <span style=\"color: #336666;\">true<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #336666;\">true<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #336666;\">true<\/span>, <span style=\"color: #336666;\">false<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ int<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;You have to read the expression <code>auto sum(T t, T2 t2)&nbsp; -&gt; decltype(t + t2)<\/code> in the following way. You express with <code>auto<\/code> that you don&#8217;t know the type and promise that you specify the type later. This specification happens in the<code> decltype<\/code> expression:<code> decltype(t + t2)<\/code>. The return type of the function template <code>sum<\/code> is that type to which the arithmetic expression evaluates. Here is what I don&#8217;t like about this C++11 syntax: You have to use two times the same expression<code> t + t2<\/code>. This is error-prone and redundant. The trailing return type syntax is in general optional but required for automatic return type deduction in C++11 and lambdas.<\/p>\n<p>Let&#8217;s see if C++14 simplifies the use of the automatic return type.<\/p>\n<\/p>\n<h2>C++14<\/h2>\n<p>With C++14, we got the convenient syntax for automatic return type deduction without redundancy.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ automaticReturnTypeTypeAuto.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;typeinfo&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;type_traits&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T2<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> sum(T t, T2 t2) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> t <span style=\"color: #555555;\">+<\/span> t2;\r\n}\r\n\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #ff6600;\">5.5<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;     <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #ff6600;\">5.5<\/span>, <span style=\"color: #336666;\">true<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #336666;\">true<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #336666;\">true<\/span>, <span style=\"color: #336666;\">false<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ int<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In C++14, you can just use <code>auto<\/code> as a return type.<\/p>\n<p>Let&#8217;s make the last jump to C++20.<\/p>\n<h2>C++20<\/h2>\n<p>In C++20, you should use instead of an unconstrained placeholder a constrained placeholder, aka concept. Defining and using the concept <code>Arithmetic<\/code> expresses explicitly my intent. Only <a href=\"https:\/\/en.cppreference.com\/w\/c\/language\/arithmetic_types\">arithmetic<\/a> types are allowed in the function template <code>sum<\/code>.<\/p>\n<p><code><\/code><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ automaticReturnTypeTypeConcepts.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;typeinfo&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;type_traits&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\nconcept Arithmetic <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>is_arithmetic<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>value;\r\n\r\nArithmetic <span style=\"color: #006699; font-weight: bold;\">auto<\/span> <span style=\"color: #cc00ff;\">sum<\/span>(Arithmetic <span style=\"color: #006699; font-weight: bold;\">auto<\/span> t, Arithmetic <span style=\"color: #006699; font-weight: bold;\">auto<\/span> t2) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> t <span style=\"color: #555555;\">+<\/span> t2;\r\n}\r\n\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #ff6600;\">5.5<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;     <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #ff6600;\">5.5<\/span>, <span style=\"color: #336666;\">true<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #336666;\">true<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ double<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(sum(<span style=\"color: #336666;\">true<\/span>, <span style=\"color: #336666;\">false<\/span>)).name() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ int<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I&#8217;m defining the concept <code>Arithmetic<\/code> by directly using the type-traits function <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/is_arithmetic\"><code>std::is_arithmetic<\/code><\/a>. The function <code>std::is_arithmetic<\/code> is a so-called compile-time predicate. A compile-time function is a function that returns at compile-time a <code>boolean<\/code>.<\/p>\n<p>If you want to read more about concepts, read my previous posts about <a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/concepts\">concepts<\/a>.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Template metaprogramming, or programming at compile time using templates, is a potent C++ technique with a bad reputation. The functions of the type-traits library, such as <code>std::common_type<\/code> or<code> std::is_arithmetic<\/code> are examples of template metaprogramming in C++. In my next post, I will elaborate more on template metaprogramming.<\/p>\n<h2>C++20 Training for Meeting C++<\/h2>\n<p>Next Tuesday (02.11.2021), I will give a <a href=\"https:\/\/meetingcpp.com\/mcpp\/training\/trainingslisting.php?tid=25\">one-day training about the big four in C++20<\/a> (Concepts, Ranges, Modules, and Coroutines). You will also get a coupon for my C++20 book when you book my training.<\/p>\n<p>&nbsp;<\/p>\n<p>I&#8217;m happy to see you,<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignleft size-full wp-image-4721\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/04\/RainerGrimmSmall.png\" alt=\"RainerGrimmSmall\" width=\"222\" height=\"66\" style=\"float: left;\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I started my discussion about the&nbsp; &#8220;Automatic Return Type (C++98)&#8221; in my last post. Today, I&#8217;m faced with the same challenge but solve it with C++11, C++14, and C++20.<\/p>\n","protected":false},"author":21,"featured_media":6233,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[436,415,437],"class_list":["post-6239","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates","tag-auto","tag-concepts","tag-decltype"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6239"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6239\/revisions"}],"predecessor-version":[{"id":6695,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6239\/revisions\/6695"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6233"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}