{"id":3566,"date":"2019-10-04T18:08:43","date_gmt":"2019-10-04T16:08:43","guid":{"rendered":"https:\/\/mariusbancila.ro\/blog\/?p=3566"},"modified":"2019-10-04T09:12:26","modified_gmt":"2019-10-04T07:12:26","slug":"concepts-versus-sfinae-based-constraints","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/","title":{"rendered":"Concepts versus SFINAE-based constraints"},"content":{"rendered":"<p>In some situations, we need to makes sure function templates can only be invoked with some specific types. SFINAE (that stands for Substitution Failure Is Not An Error) is a set of rules that specify how compilers can discard specializations from the overload resolution without causing errors. A way to achieve this is with the help of <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/enable_if\" target=\"_blank\" rel=\"noopener noreferrer\">std::enable_if<\/a>. <\/p>\n<p><!-- more --><\/p>\n<p>Let us look at an example. Suppose we want to write a function template called <tt>product()<\/tt> that returns the product of its two arguments. We only want to be able to call it with arithmetic types. Using <tt>std::enable_if<\/tt> we can define such a function as follows:<\/p>\n<pre class=\"lang:c++ decode:true \" >template &lt;typename T,\r\n          typename = typename std::enable_if_t&lt;std::is_arithmetic_v&lt;T&gt;&gt;&gt;\r\nT product(T const t1, T const t2)\r\n{\r\n   return t1 * t2;\r\n}<\/pre>\n<p>We can use it to multiply integers or doubles for example, even booleans (bools can be converted to integers, with <tt>true<\/tt> becoming 1 and <tt>false<\/tt> becoming 0), but not other types, such as <tt>std::string<\/tt>.<\/p>\n<pre class=\"lang:default decode:true \" >using namespace std::string_literals;\r\n\r\nauto v1 = product(3, 4);\r\nauto v2 = product(13.4, 2.55);\r\nauto v3 = product(false, true);\r\nauto v4 = product(\"one\"s, \"two\"s);  \/\/ error<\/pre>\n<p>The last line above would produce the following compiler error when compiling with Visual Studio: <\/p>\n<pre class=\"lang:default decode:true \" >error C2672: 'product': no matching overloaded function found\r\nerror C2783: 'T product(const T,const T)': could not deduce template argument for '&lt;unnamed-symbol&gt;'<\/pre>\n<p>SFINAE-based constraints are not the most intuitive code to read. Eventually, they model concepts with template trickery. But C++20 provides concepts as a first-class experience to make metaprogramming simpler and more expressive both to the compiler and developers. So let us look at how we can do the same and better with concepts.<\/p>\n<p>We can start by providing concepts for numerical types (a type that is either integral, or floating-point). You can see these below. Notice that the standard library provides two concepts called <tt>std::integral<\/tt> and <tt>std::floating_point<\/tt> in the header <tt>&lt;concepts&gt;<\/tt>. The implementation below is identical to the standard one.<\/p>\n<pre class=\"lang:c++ decode:true \" >template &lt;typename T&gt;\r\nconcept integral = std::is_integral_v&lt;T&gt;;\r\n\r\ntemplate &lt;typename T&gt;\r\nconcept floating_point = std::is_floating_point_v&lt;T&gt;;\r\n\r\ntemplate &lt;typename T&gt;\r\nconcept numeric = integral&lt;T&gt; || floating_point&lt;T&gt;;\r\n<\/pre>\n<p>Having this <tt>numeric<\/tt> concept available, we can change the definition of the <tt>product()<\/tt> function to the following:<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\ntemplate &lt;numeric T&gt;\r\nT product(T const t1, T const t2)\r\n{\r\n   return t1 * t2;\r\n}<\/pre>\n<p>Compiling the lines above would again produce an error for the last invocation of <tt>product()<\/tt> using <tt>std::string<\/tt> arguments. This time, the errors yield by the Visual Studio compiler are as follows:<\/p>\n<pre class=\"lang:default decode:true \" >error C2672: 'product': no matching overloaded function found\r\nerror C7602: 'product': the associated constraints are not satisfied<\/pre>\n<p>But what if we want to extend the function template <tt>product()<\/tt> so that it works for every type for which the operator* is overloaded? That&#8217;s difficult to do with SFINAE but rather straight-forward with concepts. The only thing we need to do is define a concept that expresses that. Below, this concept is called <tt>multiplicative<\/tt>.<\/p>\n<pre class=\"lang:c++ decode:true \" >template&lt;typename T&gt;\r\nconcept multiplicative = requires(const T a, const T b)\r\n{\r\n    { a * b }-&gt;T;\r\n};\r\n<\/pre>\n<p>The changes to the definition of <tt>product()<\/tt> are minimal: we just replace <tt>numeric<\/tt> with <tt>multiplicative<\/tt>.<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\ntemplate &lt;multiplicative T&gt;\r\nT product(T const t1, T const t2)\r\n{\r\n   return t1 * t2;\r\n}<\/pre>\n<p>So what can we do to make <tt>product(\"one\"s, \"two\"s)<\/tt> compile? We can overload operator* for <tt>std::string<\/tt>. The following is an implementation that &#8220;zips&#8221; two strings together. The product of &#8220;abc&#8221; and &#8220;xywz&#8221; is &#8220;axbycwz&#8221;. The actual implementation is not important; this is provided just for the sake of making the example produce some actual values.<\/p>\n<pre class=\"lang:c++ decode:true \" >std::string operator*(std::string const&amp; s1, std::string const&amp; s2)\r\n{\r\n   std::string result(s1.length() + s2.length(), '\\0');\r\n   size_t i = 0;\r\n   size_t j = 0;\r\n   while(i &lt; s1.length() &amp;&amp; i &lt; s2.length())\r\n   {\r\n      result[j++] = s1[i];\r\n      result[j++] = s2[i];\r\n      i++;\r\n   }\r\n   \r\n   for (size_t k = i; k &lt; s1.length(); ++k)\r\n      result[j++] = s1[k];\r\n\r\n   for (size_t k = i; k &lt; s2.length(); ++k)\r\n      result[j++] = s2[k];\r\n\r\n   return result;\r\n}<\/pre>\n<p>With this available the code we&#8217;ve seen above compiles without errors.<\/p>\n<pre class=\"lang:default decode:true \" >using namespace std::string_literals;\r\n\r\nauto v1 = product(3, 4);\r\nauto v2 = product(13.4, 2.55);\r\nauto v3 = product(false, true);\r\nauto v4 = product(\"one\"s, \"two\"s);<\/pre>\n<p>And that&#8217;s how simple concepts can make the code. More about the advantages of concepts can be found here: <a href=\"http:\/\/open-std.org\/JTC1\/SC22\/WG21\/docs\/papers\/2016\/p0225r0.html\" target=\"_blank\" rel=\"noopener noreferrer\">Why I want Concepts, and why I want them sooner rather than later<\/a>.<\/p>\n<p>See also on this topic:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.fluentcpp.com\/2018\/05\/15\/make-sfinae-pretty-1-what-value-sfinae-brings-to-code\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Make SFINAE Pretty \u2013 Part 1: What SFINAE Brings to Code<\/a><\/li>\n<li><a href=\"https:\/\/www.fluentcpp.com\/2018\/05\/18\/make-sfinae-pretty-2-hidden-beauty-sfinae\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Make SFINAE Pretty \u2013 Part 2: the Hidden Beauty of SFINAE<\/a><\/li>\n<li><a href=\"https:\/\/akrzemi1.wordpress.com\/2016\/09\/21\/concepts-lite-vs-enable_if\/\" target=\"_blank\" rel=\"noopener noreferrer\">Concepts Lite vs enable_if<\/a><\/li>\n<li><a href=\"https:\/\/www.jakubkonka.com\/2017\/09\/02\/type-traits-cpp.html\" target=\"_blank\" rel=\"noopener noreferrer\">C++ short stories: type traits, concepts, and type constraints<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In some situations, we need to makes sure function templates can only be invoked with some specific types. SFINAE (that stands for Substitution Failure Is Not An Error) is a set of rules that specify how compilers can discard specializations from the overload resolution without causing errors. A way to achieve this is with the help of <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/enable_if\" target=\"_blank\" rel=\"noopener noreferrer\">std::enable_if<\/a>. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_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":[7],"tags":[451,543,153,607],"class_list":["post-3566","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-c20","tag-concepts","tag-sfinae"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"See how C++20 concepts can help writing code that is simpler and more expressive than using std::enable_if and SFINAE.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Marius Bancila\"\/>\n\t<meta name=\"keywords\" content=\"c++,c++20,concepts,sfinae\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Marius Bancila&#039;s Blog | About code. Mostly on C++\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Concepts versus SFINAE-based constraints\" \/>\n\t\t<meta property=\"og:description\" content=\"See how C++20 concepts can help writing code that is simpler and more expressive than using std::enable_if and SFINAE.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2019-10-04T16:08:43+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2019-10-04T07:12:26+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Concepts versus SFINAE-based constraints\" \/>\n\t\t<meta name=\"twitter:description\" content=\"See how C++20 concepts can help writing code that is simpler and more expressive than using std::enable_if and SFINAE.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/10\\\/04\\\/concepts-versus-sfinae-based-constraints\\\/#article\",\"name\":\"Concepts versus SFINAE-based constraints\",\"headline\":\"Concepts versus SFINAE-based constraints\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"datePublished\":\"2019-10-04T18:08:43+02:00\",\"dateModified\":\"2019-10-04T09:12:26+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":2,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/10\\\/04\\\/concepts-versus-sfinae-based-constraints\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/10\\\/04\\\/concepts-versus-sfinae-based-constraints\\\/#webpage\"},\"articleSection\":\"C++, C++, c++20, concepts, SFINAE\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/10\\\/04\\\/concepts-versus-sfinae-based-constraints\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"position\":2,\"name\":\"IT\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"position\":3,\"name\":\"Software\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"position\":4,\"name\":\"C++\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/10\\\/04\\\/concepts-versus-sfinae-based-constraints\\\/#listItem\",\"name\":\"Concepts versus SFINAE-based constraints\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/10\\\/04\\\/concepts-versus-sfinae-based-constraints\\\/#listItem\",\"position\":5,\"name\":\"Concepts versus SFINAE-based constraints\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Marius Bancila\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/10\\\/04\\\/concepts-versus-sfinae-based-constraints\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Marius Bancila\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/10\\\/04\\\/concepts-versus-sfinae-based-constraints\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/10\\\/04\\\/concepts-versus-sfinae-based-constraints\\\/\",\"name\":\"Concepts versus SFINAE-based constraints\",\"description\":\"See how C++20 concepts can help writing code that is simpler and more expressive than using std::enable_if and SFINAE.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2019\\\/10\\\/04\\\/concepts-versus-sfinae-based-constraints\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2019-10-04T18:08:43+02:00\",\"dateModified\":\"2019-10-04T09:12:26+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Concepts versus SFINAE-based constraints","description":"See how C++20 concepts can help writing code that is simpler and more expressive than using std::enable_if and SFINAE.","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/","robots":"max-image-preview:large","keywords":"c++,c++20,concepts,sfinae","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/#article","name":"Concepts versus SFINAE-based constraints","headline":"Concepts versus SFINAE-based constraints","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"datePublished":"2019-10-04T18:08:43+02:00","dateModified":"2019-10-04T09:12:26+02:00","inLanguage":"en-US","commentCount":2,"mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/#webpage"},"articleSection":"C++, C++, c++20, concepts, SFINAE"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog#listItem","position":1,"name":"Home","item":"https:\/\/mariusbancila.ro\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","position":2,"name":"IT","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","position":3,"name":"Software","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","position":4,"name":"C++","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/#listItem","name":"Concepts versus SFINAE-based constraints"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/#listItem","position":5,"name":"Concepts versus SFINAE-based constraints","previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"}}]},{"@type":"Organization","@id":"https:\/\/mariusbancila.ro\/blog\/#organization","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","url":"https:\/\/mariusbancila.ro\/blog\/"},{"@type":"Person","@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author","url":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/","name":"Marius Bancila","image":{"@type":"ImageObject","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Marius Bancila"}},{"@type":"WebPage","@id":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/","name":"Concepts versus SFINAE-based constraints","description":"See how C++20 concepts can help writing code that is simpler and more expressive than using std::enable_if and SFINAE.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2019-10-04T18:08:43+02:00","dateModified":"2019-10-04T09:12:26+02:00"},{"@type":"WebSite","@id":"https:\/\/mariusbancila.ro\/blog\/#website","url":"https:\/\/mariusbancila.ro\/blog\/","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","inLanguage":"en-US","publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Marius Bancila's Blog | About code. Mostly on C++","og:type":"article","og:title":"Concepts versus SFINAE-based constraints","og:description":"See how C++20 concepts can help writing code that is simpler and more expressive than using std::enable_if and SFINAE.","og:url":"https:\/\/mariusbancila.ro\/blog\/2019\/10\/04\/concepts-versus-sfinae-based-constraints\/","article:published_time":"2019-10-04T16:08:43+00:00","article:modified_time":"2019-10-04T07:12:26+00:00","twitter:card":"summary","twitter:title":"Concepts versus SFINAE-based constraints","twitter:description":"See how C++20 concepts can help writing code that is simpler and more expressive than using std::enable_if and SFINAE."},"aioseo_meta_data":{"post_id":"3566","title":"Concepts versus SFINAE-based constraints","description":"See how C++20 concepts can help writing code that is simpler and more expressive than using std::enable_if and SFINAE.","keywords":[{"label":"C++","value":"C++"},{"label":"C++20","value":"C++20"},{"label":"concepts","value":"concepts"},{"label":"SFINAE","value":"SFINAE"}],"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-18 21:03:35","updated":"2025-12-12 07:53:21","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"author_meta":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"featured_img":null,"jetpack_publicize_connections":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"uagb_comment_info":2,"uagb_excerpt":"In some situations, we need to makes sure function templates can only be invoked with some specific types. SFINAE (that stands for Substitution Failure Is Not An Error) is a set of rules that specify how compilers can discard specializations from the overload resolution without causing errors. A way to achieve this is with the&hellip;","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">c++20<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">concepts<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">SFINAE<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">c++20<\/span>","<span class=\"advgb-post-tax-term\">concepts<\/span>","<span class=\"advgb-post-tax-term\">SFINAE<\/span>"]}},"comment_count":"2","relative_dates":{"created":"Posted 7 years ago","modified":"Updated 7 years ago"},"absolute_dates":{"created":"Posted on October 4, 2019","modified":"Updated on October 4, 2019"},"absolute_dates_time":{"created":"Posted on October 4, 2019 6:08 pm","modified":"Updated on October 4, 2019 9:12 am"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-Vw","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/3566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/comments?post=3566"}],"version-history":[{"count":9,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/3566\/revisions"}],"predecessor-version":[{"id":3575,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/3566\/revisions\/3575"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=3566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=3566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=3566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}