{"id":4179,"date":"2021-03-15T10:17:43","date_gmt":"2021-03-15T08:17:43","guid":{"rendered":"https:\/\/mariusbancila.ro\/blog\/?p=4179"},"modified":"2021-03-19T15:53:03","modified_gmt":"2021-03-19T13:53:03","slug":"typename-or-class","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/","title":{"rendered":"The choice between typename and class"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When working with C++ templates, you have probably seen <code>typename<\/code> and <code>class<\/code> used interchangeably. Is there a difference between them? This post will explain when these two keywords can be used in templates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s consider the following example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;class T>\nclass foo {};\n\ntemplate &lt;typename T>\nclass foo {};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this context, when declaring a <em>type template parameter<\/em>, there is no difference, they are interchangeable. They can also be mixed together, as in the following example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template&lt;class A, typename B>\nstruct foo\n{\n   A a;\n   B b;\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here is what the C++ standard has to say about it (\u00a713.2.2):<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>There is no semantic difference between class and typename in a type-parameter-key.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">So which one should be used then? It&#8217;s all a matter of style. In fact, the standard itself is using both of them in what seems to be a completely random style, which I believe actually depended on the preferred style of the people that wrote the proposals that made it to the standard.<\/p>\n\n\n\n<p class=\"has-light-green-cyan-background-color has-background wp-block-paragraph\">You can find <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/links\">here<\/a> links to various version of the C++ standard if you want to have a look.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A recent poll I run on twitter with more than 100 respondents showed that 9 out of 10 people prefer to use <tt>typename<\/tt> instead of <tt>class<\/tt>. I personally prefer the same.<br><a href=\"https:\/\/twitter.com\/mariusbancila\/status\/1369541914601652226\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4198\" src=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2021\/03\/twitterpoll.png\" alt=\"\" width=\"562\" height=\"283\" srcset=\"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2021\/03\/twitterpoll.png 562w, https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2021\/03\/twitterpoll-300x151.png 300w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, there are cases when these two cannot be used interchangeably. One case is <em>dependent types<\/em>, which are names that are dependent on a template parameter. Here is an example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T>\nstruct wrapper\n{\n   using value_type = T;\n\n   value_type value;\n};\n\ntemplate &lt;typename T>\nstruct foo\n{\n   T wrapped_value;\n\n   typename T::value_type get_wrapped_value() { return wrapped_value.value; }\n};\n\nint main()\n{\n   foo&lt;wrapper&lt;int>> f{ {42} };\n   std::cout &lt;&lt; f.get_wrapped_value() &lt;&lt; '\\n';\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this snippet, <code>foo<\/code> is a class that contains an object of a type that wraps another value and contains a public data member called <code>value<\/code>. <code>wrapper<\/code> is such a type. However, <code>foo<\/code> has a method that returns the wrapped value. That is <code>T::value_type<\/code>. However, if you use it without the <code>typename<\/code> keyword, as seen in the snippet, you get a compiler error. The following is from the VC++ compiler:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">warning C4346: 'value_type': dependent name is not a type\nmessage : prefix with 'typename' to indicate a type\nerror C2061: syntax error: identifier 'value_type'\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is where you must use <code>typename<\/code> but where <code>class<\/code> is not allowed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following is an alternative solution where an alias is introduced in the <code>foo<\/code> class template, which, of course, requires the <code>typename<\/code> keyword.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T>\nstruct foo\n{\n   using wrapped_value_type = typename T::value_type;\n\n   T wrapped_value;\n\n   wrapped_value_type get_wrapped_value() { return wrapped_value.value; }\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As a parenthesis, there is another alternative solution to this particular problem from this example (given than we only need the wrapped value type for the return type of a function). That is the use of auto for the return type.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T>\nstruct foo\n{\n   T wrapped_value;\n\n   auto get_wrapped_value() { return wrapped_value.value; }\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Prior to C++17, there was another case where these two could not be used interchangeably. It is the case of <em>template template parameters<\/em>, where <tt>class<\/tt> had to be used. Let&#8217;s look at an example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, consider there is another class template that has two type parameters, as shown in the following snippet.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T, typename U>\nstruct dual_wrapper\n{\n   using value_type1 = T;\n   using value_type2 = U;\n\n   value_type1 value;\n   value_type2 another_value;\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Having the <code>foo<\/code> class template from the previous example, we could write the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">foo&lt;wrapper&lt;int>> f{ {42} };\nstd::cout &lt;&lt; f.get_wrapped_value() &lt;&lt; '\\n';\n   \nfoo&lt;dual_wrapper&lt;int, double>> f2{ {43, 15.0} };\nstd::cout &lt;&lt; f2.get_wrapped_value() &lt;&lt; '\\n';<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, what if you want to restrict the instantiation to wrappers that have a single type parameter? Then, you can modify the <code>foo<\/code> class template as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename V, template &lt;typename> class T>\nstruct foo\n{\n   T&lt;V> wrapped_value;\n\n   auto get_wrapped_value() { return wrapped_value.value; }\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>template &lt;typename&gt; class T<\/code> part is a template template parameter. It used to require the keyword <code>class<\/code> but as of C++17, <code>typename<\/code> can be used here to, as in <code>template &lt;typename&gt; typename T<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We need to change a bit the way objects of type <code>foo<\/code> are declared. However, attempting to use <code>dual_wrapper<\/code> now results in a compiler error.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">foo&lt;int, wrapper> f{ {42} };\nstd::cout &lt;&lt; f.get_wrapped_value() &lt;&lt; '\\n';\n   \nfoo&lt;int, dual_wrapper> f2{ {43, 15.0} };     \/\/ error<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Can anything else be used in place of a the <code>typename<\/code> or <code>class<\/code> keywords? As of C++20, when declaring a type template parameter, the answer is yes. A concept name can be used instead. Here is an example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T>\nconcept Numeric = std::is_arithmetic_v&lt;T>;\n\ntemplate &lt;Numeric T>\nstruct wrapper\n{\n   T value;\n};\n\nwrapper&lt;int> vi{ 42 };\nwrapper&lt;std::string> vs{ \"42\"s }; \/\/ error: 'wrapper': the associated constraints are not satisfied<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this snippet, <code>Numeric<\/code> is a concept used to ensure that the <code>wrapper<\/code> class template can only be instantiated with numerical types, such as <code>int<\/code> or <code>double<\/code>. The type template parameter has the form <code>Numeric T<\/code> instead of <code>class T<\/code> or <code>typename T<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is an alternative syntax to declaring the wrapper class template with the same semantics. This is shown below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T> requires Numeric&lt;T>\nstruct wrapper\n{\n   T value;\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We have discussed so far type template parameters and template template parameters. However, there is a 3rd category of template parameters, <em>non-type template parameters<\/em>. These ones are introduced not with <code>typename<\/code>, <code>class<\/code>, or the name of a concept, but with the name of a <em>structural type<\/em> which can be a lvalue reference types, an integral type, a pointer type, a pointer to member type, an enumeration type, <code>std::nullptr_t<\/code>, and, as of C++20, a floating-point type, or a literal class type that satisfies some conditions. Here are some examples:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T, size_t Size>\nstruct fixed_size_array\n{\n   T[Size] values;\n};\n\nfixed_size_array&lt;int, 4> arr;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The placeholder <code>auto<\/code> can be used instead of the actual type, with the forms <code>auto<\/code>, <code>auto**<\/code>, <code>auto&amp;<\/code> and <code>decltype(auto)<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;auto V>\nstruct foo \n{\n   decltype(V) const value = V;\n};\n\nfoo&lt;42> f1;\nstd::cout &lt;&lt; f1.value &lt;&lt; '\\n';\n\nfoo&lt;42.0> f2;\nstd::cout &lt;&lt; f2.value &lt;&lt; '\\n';<\/pre>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p class=\"wp-block-paragraph\">To summarize all this:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>when declaring type template parameters, use either&nbsp;<code>typename<\/code>&nbsp;or&nbsp;<code>class<\/code>, or the name of a concept<\/li><li>when declaring template template parameters, use either&nbsp;<code>typename<\/code>&nbsp;or&nbsp;<code>class<\/code>&nbsp;if you\u2019re using at least C++17, or&nbsp;class&nbsp;for a previous standard version<\/li><li>when declaring non-type template parameters, use the name of a structural type, or the placeholder&nbsp;<code>auto<\/code>&nbsp;or&nbsp;<code>decltype(auto)<\/code><\/li><li>when declaring dependent types use&nbsp;<code>typename<\/code><\/li><\/ul>\n<\/div><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>When working with C++ templates, you have probably seen typename and class used interchangeably. Is there a difference between them? This post will explain when these two keywords can be used in templates. Let&#8217;s consider the following example: In this context, when declaring a type template parameter, there is no difference, they are interchangeable. They &#8230; <a title=\"The choice between typename and class\" class=\"read-more\" href=\"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/\" aria-label=\"Read more about The choice between typename and class\">Read more<\/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,323],"class_list":["post-4179","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-templates"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"This post explains when to use the typename and class keyword when writing templates in C++.\" \/>\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,c++17,templates\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/\" \/>\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=\"The choice between typename and class\" \/>\n\t\t<meta property=\"og:description\" content=\"This post explains when to use the typename and class keyword when writing templates in C++.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2021-03-15T08:17:43+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2021-03-19T13:53:03+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"The choice between typename and class\" \/>\n\t\t<meta name=\"twitter:description\" content=\"This post explains when to use the typename and class keyword when writing templates in C++.\" \/>\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\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/#article\",\"name\":\"The choice between typename and class\",\"headline\":\"The choice between typename and class\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/twitterpoll.png\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/#articleImage\",\"width\":562,\"height\":283},\"datePublished\":\"2021-03-15T10:17:43+02:00\",\"dateModified\":\"2021-03-19T15:53:03+02:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/#webpage\"},\"articleSection\":\"C++, C++, templates\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/#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\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/#listItem\",\"name\":\"The choice between typename and class\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/#listItem\",\"position\":5,\"name\":\"The choice between typename and class\",\"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\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/#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\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/\",\"name\":\"The choice between typename and class\",\"description\":\"This post explains when to use the typename and class keyword when writing templates in C++.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/15\\\/typename-or-class\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2021-03-15T10:17:43+02:00\",\"dateModified\":\"2021-03-19T15:53:03+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":"The choice between typename and class","description":"This post explains when to use the typename and class keyword when writing templates in C++.","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/","robots":"max-image-preview:large","keywords":"c++,c++20,c++17,templates","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/#article","name":"The choice between typename and class","headline":"The choice between typename and class","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/mariusbancila.ro\/blog\/wp-content\/uploads\/2021\/03\/twitterpoll.png","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/#articleImage","width":562,"height":283},"datePublished":"2021-03-15T10:17:43+02:00","dateModified":"2021-03-19T15:53:03+02:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/#webpage"},"articleSection":"C++, C++, templates"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/#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\/2021\/03\/15\/typename-or-class\/#listItem","name":"The choice between typename and class"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/#listItem","position":5,"name":"The choice between typename and class","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\/2021\/03\/15\/typename-or-class\/#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\/2021\/03\/15\/typename-or-class\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/","name":"The choice between typename and class","description":"This post explains when to use the typename and class keyword when writing templates in C++.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2021-03-15T10:17:43+02:00","dateModified":"2021-03-19T15:53:03+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":"The choice between typename and class","og:description":"This post explains when to use the typename and class keyword when writing templates in C++.","og:url":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/15\/typename-or-class\/","article:published_time":"2021-03-15T08:17:43+00:00","article:modified_time":"2021-03-19T13:53:03+00:00","twitter:card":"summary","twitter:title":"The choice between typename and class","twitter:description":"This post explains when to use the typename and class keyword when writing templates in C++."},"aioseo_meta_data":{"post_id":"4179","title":"The choice between typename and class","description":"This post explains when to use the typename and class keyword when writing templates in C++.","keywords":[{"label":"C++","value":"C++"},{"label":"C++20","value":"C++20"},{"label":"C++17","value":"C++17"},{"label":"templates","value":"templates"}],"keyphrases":{"focus":[],"additional":[]},"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":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"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":{"id":"aioseo-article-636965220d1be","slug":"article","graphName":"Article","label":"Article","properties":{"type":"BlogPosting","name":"#post_title","headline":"#post_title","description":"#post_excerpt","image":"","keywords":"","author":{"name":"#author_name","url":"#author_url"},"dates":{"include":true,"datePublished":"","dateModified":""}}},"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","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":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":{"businessInfo":{"name":"","urls":{"website":"","aboutPage":"","contactPage":""},"address":{"line1":"","line2":"","zip":"","city":"","state":"","country":""},"contact":{"email":"","phone":"","fax":""},"ids":{"vatID":"","taxID":"","chamberID":""},"payment":{"priceIndication":"","currenciesAccepted":"","methodsAccepted":""},"areaServed":""},"openingHours":{"show":false,"closedLabel":"","open24h":false,"open24hLabel":"","open247":false,"use24hFormat":false,"twoSets":false,"timezone":"","hours":[]}},"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-18 21:02:54","updated":"2025-12-12 07:56:25","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":0,"uagb_excerpt":"When working with C++ templates, you have probably seen typename and class used interchangeably. Is there a difference between them? This post will explain when these two keywords can be used in templates. Let&#8217;s consider the following example: In this context, when declaring a type template parameter, there is no difference, they are interchangeable. They&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\">templates<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">templates<\/span>"]}},"comment_count":"0","relative_dates":{"created":"Posted 6 years ago","modified":"Updated 6 years ago"},"absolute_dates":{"created":"Posted on March 15, 2021","modified":"Updated on March 19, 2021"},"absolute_dates_time":{"created":"Posted on March 15, 2021 10:17 am","modified":"Updated on March 19, 2021 3:53 pm"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-15p","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4179","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=4179"}],"version-history":[{"count":23,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4179\/revisions"}],"predecessor-version":[{"id":4250,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4179\/revisions\/4250"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=4179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=4179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=4179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}