{"id":27526,"date":"2021-02-04T15:00:50","date_gmt":"2021-02-04T15:00:50","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=27526"},"modified":"2021-02-04T16:25:08","modified_gmt":"2021-02-04T16:25:08","slug":"abbreviated-function-templates-and-constrained-auto","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/abbreviated-function-templates-and-constrained-auto\/","title":{"rendered":"Abbreviated Function Templates and Constrained Auto"},"content":{"rendered":"<p>Declaring function templates in C++ has always been quite verbose. C++20 added a new way of doing so that is more terse and more consistent with lambdas: abbreviated function templates. This short post will show how to use this syntax and how it applies to C++20 concepts.<\/p>\n<h2>Abbreviated Function Templates<\/h2>\n<p>C++11 introduced lambdas, which look like this:<\/p>\n<pre class=\"prettyprint\">[captures] (type_1 param_1, type_2 param_2) { body(param_1, param_2); }\r\n<\/pre>\n<p>You can only call this lambdas with arguments of <code>type_1<\/code> and <code>type_2<\/code>. However we frequently use lambdas in situations where the types would be difficult to spell out in full (especially when using features like <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/ranges\">ranges<\/a>). C++14 allowed you to make lambdas which can be called with arguments of any type by using the <code>auto<\/code> keyword:<\/p>\n<pre class=\"prettyprint\">[captures] (auto param_1, auto param_2) { body(param_1, param_2); }<\/pre>\n<p>Now you can pass any types as the arguments. C++20&#8217;s abbreviated function templates allows you to apply this kind of syntax to function templates.<\/p>\n<p>In C++17 you might write a function to give animals head scratches as a function template, so it can be called with any type of animal:<\/p>\n<pre class=\"prettyprint\">template &lt;class Animal&gt;\r\nvoid give_head_scratches (Animal const&amp; the_animal);<\/pre>\n<p>In C++20 you can simplify this using <code>auto<\/code>:<\/p>\n<pre class=\"prettyprint\">void give_head_scratches (auto const&amp; the_animal);<\/pre>\n<p>This version is less verbose, requires coming up with fewer names, and is more consistent with C++14 lambdas.<\/p>\n<h2>Constrained Auto<\/h2>\n<p>There&#8217;s a problem with the above function template though: according to the declaration we can pass literally anything to it. We can happily make calls that look like this:<\/p>\n<pre class=\"prettyprint\">give_head_scratches(42);\r\ngive_head_scratches(a_cactus);\r\ngive_head_scratches(blog_post);\r\ngive_head_scratches(the_platonic_ideal_of_a_chair);<\/pre>\n<p>They might compile and do something weird, or they might fail to compile due to the implementation of the template doing something which those types don&#8217;t support. Ideally we&#8217;d want to both document the interface of this function template with what kind of types it supports and also give the compiler the ability to give detailed errors when the declaration is instantiated with in incompatible type.<\/p>\n<p>C++20 gives us <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/c20-concepts-are-here-in-visual-studio-2019-version-16-3\/\">Concepts<\/a> to help solve this problem. If we have some <code>animal<\/code> concept which defines what interface a type representing an animal should have then we can use it like so:<\/p>\n<pre class=\"prettyprint\">template &lt;animal Animal&gt;\r\nvoid give_head_scratches (Animal const&amp; the_animal);<\/pre>\n<p>This is quite verbose and repetitive. Ideally we&#8217;d be able to use the concept name directly in the function parameter list like this:<\/p>\n<pre class=\"prettyprint\">void give_head_scratches (animal const&amp; the_animal);<\/pre>\n<p>However, this syntax was rejected from standardization, because you can&#8217;t tell whether this is a function template or a regular function without knowing whether <code>animal<\/code> is a type or a concept.<\/p>\n<p>Fortunately, a version of this syntax was included in C++20 which uses the <code>auto<\/code> keyword again:<\/p>\n<pre class=\"prettyprint\">void give_head_scratches (animal auto const&amp; the_animal);<\/pre>\n<p>This checks that whatever is substituted for <code>auto<\/code> satisfies the <code>animal<\/code> concept. So if we instantiate the template with a <code>kitten<\/code> then <code>animal&lt;kitten&gt;<\/code> will be checked. This gives us back our terse syntax while also allowing us to constrain our template declarations.<\/p>\n<h2>Try Them Out!<\/h2>\n<p>Constrained auto has been supported in MSVC since <a href=\"https:\/\/visualstudio.microsoft.com\/downloads\/\">Visual Studio 2019 version 16.8,<\/a> and abbreviated function templates have recently been added to <a href=\"https:\/\/visualstudio.microsoft.com\/vs\/preview\/\">version 16.9 Preview 3<\/a>. We encourage you to download the tools and give the implementations a try! We can be reached via the comments below or via email (visualcpp@microsoft.com). If you find any problems you can use the <a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio?view=vs-2019\">Report a Problem<\/a> tool in Visual Studio or head over to the <a href=\"https:\/\/developercommunity.visualstudio.com\/spaces\/62\/index.html\">Visual Studio Developer Community<\/a>. You can also find us on Twitter <a href=\"https:\/\/twitter.com\/visualc\">@VisualC<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Declaring function templates in C++ has always been quite verbose. C++20 added a new way of doing so that is more terse and more consistent with lambdas: abbreviated function templates. This short post will show how to use this syntax and how it applies to C++20 concepts. Abbreviated Function Templates C++11 introduced lambdas, which look [&hellip;]<\/p>\n","protected":false},"author":706,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-27526","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Declaring function templates in C++ has always been quite verbose. C++20 added a new way of doing so that is more terse and more consistent with lambdas: abbreviated function templates. This short post will show how to use this syntax and how it applies to C++20 concepts. Abbreviated Function Templates C++11 introduced lambdas, which look [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/27526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/706"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=27526"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/27526\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=27526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=27526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=27526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}