{"id":4993,"date":"2016-10-20T20:43:19","date_gmt":"2016-10-20T20:43:19","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/compare-and-modify-types\/"},"modified":"2023-06-26T12:38:41","modified_gmt":"2023-06-26T12:38:41","slug":"compare-and-modify-types","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/compare-and-modify-types\/","title":{"rendered":"Compare and Modify Types"},"content":{"rendered":"<p>The type-traits library empowers you to compare and modify types. All is done at compile time therefore, there is no performance penalty.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h2>Comparing types<\/h2>\n<p>The type-traits library supports three kinds of comparisons:<\/p>\n<ul>\n<li><span style=\"font-family: courier new,courier;\">is_base_of&lt;Base, Derived&gt;<\/span><\/li>\n<li><span style=\"font-family: courier new,courier;\">is_convertible&lt;From, To&gt;<\/span><\/li>\n<li><span style=\"font-family: courier new,courier;\">is_same&lt;T, U&gt;<\/span><\/li>\n<\/ul>\n<p>Thanks to its member <span style=\"font-family: courier new,courier;\">value<\/span>, each class template returns true or false and is the optimal fit for <span style=\"font-family: courier new,courier;\"><a href=\"https:\/\/www.modernescpp.com\/index.php\/statically-checked\">static_assert.<\/a><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ compare.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;cstdint&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;type_traits&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Base<\/span>{};\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Derived<\/span>: <span style=\"color: #0000ff;\">public<\/span> Base{};\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n  \r\n  std::cout &lt;&lt; std::boolalpha &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_base_of&lt;Base,Derived&gt;::value: \"<\/span> &lt;&lt; std::is_base_of&lt;Base,Derived&gt;::value &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_base_of&lt;Derived,Base&gt;::value: \"<\/span> &lt;&lt; std::is_base_of&lt;Derived,Base&gt;::value &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_base_of&lt;Derived,Derived&gt;::value: \"<\/span> &lt;&lt; std::is_base_of&lt;Derived,Derived&gt;::value &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #008000;\">\/\/ static_assert(std::is_base_of&lt;Derived,Base&gt;::value,\"Derived is not base of Base\");<\/span>\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_convertible&lt;Base*,Derived*&gt;::value: \"<\/span> &lt;&lt; std::is_convertible&lt;Base*,Derived*&gt;::value &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_convertible&lt;Derived*,Base*&gt;::value: \"<\/span> &lt;&lt; std::is_convertible&lt;Derived*,Base*&gt;::value &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_convertible&lt;Derived*,Derived*&gt;::value: \"<\/span> &lt;&lt; std::is_convertible&lt;Derived*,Derived*&gt;::value &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #008000;\">\/\/ static_assert(std::is_convertible&lt;Base*,Derived*&gt;::value,\"Base* can not be converted to Derived*\");<\/span>\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_same&lt;int, int32_t&gt;::value: \"<\/span> &lt;&lt; std::is_same&lt;<span style=\"color: #2b91af;\">int<\/span>, <span style=\"color: #2b91af;\">int32_t<\/span>&gt;::value &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_same&lt;int, int64_t&gt;::value: \"<\/span> &lt;&lt; std::is_same&lt;<span style=\"color: #2b91af;\">int<\/span>, <span style=\"color: #2b91af;\">int64_t<\/span>&gt;::value &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_same&lt;long int, int64_t&gt;::value: \"<\/span> &lt;&lt; std::is_same&lt;<span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">int<\/span>, <span style=\"color: #2b91af;\">int64_t<\/span>&gt;::value &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #008000;\">\/\/ static_assert(std::is_same&lt;int, int64_t&gt;::value,\"int is not the same type as int64_t\");<\/span>\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n  \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">&nbsp;<\/div>\n<p>&nbsp;<\/p>\n<p>The output of the program should not surprise you.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4990\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/compare.png\" alt=\"compare\" width=\"632\" height=\"333\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/compare.png 632w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/compare-300x158.png 300w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/p>\n<p>If I use the <span style=\"font-family: courier new,courier;\">static_asser<\/span>t in lines 18, 26, and 34, the assertion will fire at compile time.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4991\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/compareStaticAssert.png\" alt=\"compareStaticAssert\" width=\"800\" height=\"267\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/compareStaticAssert.png 856w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/compareStaticAssert-300x100.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/compareStaticAssert-768x257.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>Modifying types<\/h2>\n<p>Now I&#8217;m a little bit pedantic. However, the C++ standard speaks about the modification or transformation of types that are not accurate. At compile time, there is no state. Therefore, there is nothing to modify. You can only generate new types on request. The type-traits library is<a href=\"https:\/\/en.wikipedia.org\/wiki\/Template_metaprogramming\"> template metaprogramming <\/a>in a very beautiful robe. Template metaprogramming is a purely functional language that is embedded in C++. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Purely_functional_programming\">Purely functional languages<\/a> have no state. I said that I will continue to speak about modifying types in the rest of this post.<\/p>\n<p>The type-traits library has many functions to modify types at compile time. Therefore, you can remove <span style=\"font-family: courier new,courier;\">or add const or volatile properties from a type<\/span>. But there is more: Remove the sign of a type or the dimension of an array; change the pointer or reference properties of y type.<\/p>\n<p>Here is the overview:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">    <span style=\"color: #008000;\">\/\/ const-volatile modifications<\/span>\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> remove_const;\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> remove_volatile;\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> remove_cv;\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> add_const;\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> add_volatile;\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> add_cv;\r\n    \r\n    <span style=\"color: #008000;\">\/\/ reference modifications<\/span>\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> remove_reference;\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> add_lvalue_reference;\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> add_rvalue_reference;\r\n    \r\n    <span style=\"color: #008000;\">\/\/ sign modifications<\/span>\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> make_signed;\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> make_unsigned;\r\n    \r\n    <span style=\"color: #008000;\">\/\/ array modifications<\/span>\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> remove_extent;\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> remove_all_extents;\r\n    \r\n    <span style=\"color: #008000;\">\/\/ pointer modifications<\/span>\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> remove_pointer;\r\n    <span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt; <span style=\"color: #0000ff;\">struct<\/span> add_pointer;\r\n    \r\n<\/pre>\n<\/div>\n<p>To get from a reference <span style=\"font-family: courier new,courier;\">int&amp;<\/span> at compile time the type <span style=\"font-family: courier new,courier;\">int<\/span>, you have to use the member <span style=\"font-family: courier new,courier;\">type<\/span> of the class template. In C++14, this becomes a lot easier. You have only to add _t to the function. That holds for all invocated functions of this section.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">std::cout &lt;&lt; std::is_same&lt;<span style=\"color: #2b91af;\">int<\/span>,std::remove_reference&lt;<span style=\"color: #2b91af;\">int<\/span> &amp;&gt;::type&gt;::value &lt;&lt; std::endl; <span style=\"color: #008000;\">\/\/ true<\/span><\/div>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">std::cout &lt;&lt; std::is_same&lt;<span style=\"color: #2b91af;\">int<\/span>,std::<span style=\"color: #2b91af;\">remove_reference_t<\/span>&lt;<span style=\"color: #2b91af;\">int<\/span> &amp;&gt;&gt;::value &lt;&lt; std::endl; <span style=\"color: #008000;\">\/\/ true<\/span><\/div>\n<p>&nbsp;<\/p>\n<p>The key of the code snippet is that you can write with C++14 <span style=\"font-family: courier new,courier;\">std::remove_reference&lt;int &amp;&gt;:: type<\/span> simply in the form<span style=\"font-family: courier new,courier;\"> std::remove_reference_t&lt;int &amp;&gt;<\/span>. Thanks to value, you get the result of the comparison <span style=\"font-family: courier new,courier;\">std::is_same<\/span><span style=\"font-family: courier new,courier;\"><\/span><span style=\"color: #2b91af;\"><\/span><\/p>\n<p>For completeness, I will mention that I should write about the modifications <span style=\"font-family: courier new,courier;\">std::conditional, std::common_type, a<span style=\"font-family: arial,helvetica,sans-serif;\">nd <\/span>std::enable_if.<\/span> But I don&#8217;t want to repeat myself. I presented the three functions in the post <a href=\"https:\/\/www.modernescpp.com\/index.php\/statically-checked\">Statically checked<\/a>.&nbsp; To get the rest of the details, read the Miscellaneous section<span id=\"Miscellaneous_transformations\" class=\"mw-headline\"><em> transformations <\/em>on the page <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/header\/type_traits\">cppreference.com.<br \/><\/a><\/span><\/p>\n<p><span id=\"Miscellaneous_transformations\" class=\"mw-headline\">&nbsp;One question is still open.<\/span><\/p>\n<h2>How does the whole magic work?<\/h2>\n<p>Due to a little bit of template metaprogramming, I can easily implement the class templates is_same and remove_const. I use the namespace rgr to distinguish my implementation from the C++ implementation.<br \/><span style=\"font-family: courier new,courier;\"><\/span><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ removeConst.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;type_traits&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">namespace<\/span> rgr{\r\n  \r\n  <span style=\"color: #0000ff;\">template<\/span>&lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>, <span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">U<\/span>&gt;\r\n  <span style=\"color: #0000ff;\">struct<\/span> is_same : std::false_type {};\r\n \r\n  <span style=\"color: #0000ff;\">template<\/span>&lt;<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span>&gt;\r\n  <span style=\"color: #0000ff;\">struct<\/span> is_same&lt;T, T&gt; : std::true_type {};\r\n\r\n  <span style=\"color: #0000ff;\">template<\/span>&lt; <span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span> &gt; \r\n  <span style=\"color: #0000ff;\">struct<\/span> remove_const{ \r\n    <span style=\"color: #0000ff;\">typedef<\/span> T type; \r\n  };\r\n\r\n  <span style=\"color: #0000ff;\">template<\/span>&lt; <span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">T<\/span> &gt; \r\n  <span style=\"color: #0000ff;\">struct<\/span> remove_const&lt;<span style=\"color: #0000ff;\">const<\/span> T&gt; { \r\n    <span style=\"color: #0000ff;\">typedef<\/span> T type; \r\n  };\r\n}\r\n\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n  \r\n  std::cout &lt;&lt; std::boolalpha &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; std::is_same&lt;<span style=\"color: #2b91af;\">int<\/span>,std::remove_const&lt;<span style=\"color: #0000ff;\">const<\/span> <span style=\"color: #2b91af;\">int<\/span>&gt;::type&gt;::value &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; rgr::is_same&lt;<span style=\"color: #2b91af;\">int<\/span>,rgr::remove_const&lt;<span style=\"color: #0000ff;\">const<\/span> <span style=\"color: #2b91af;\">int<\/span>&gt;::type&gt;::value &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #0000ff;\">typedef<\/span> rgr::remove_const&lt;<span style=\"color: #2b91af;\">double<\/span>&gt;::type myDouble;\r\n  std::cout &lt;&lt; rgr::is_same&lt;<span style=\"color: #2b91af;\">double<\/span>,myDouble&gt;::value &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #0000ff;\">typedef<\/span> rgr::remove_const&lt;<span style=\"color: #0000ff;\">const<\/span> std::string&gt;::type myString;\r\n  std::cout &lt;&lt; rgr::is_same&lt;std::string,myString&gt;::value &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #0000ff;\">typedef<\/span> rgr::remove_const&lt;std::add_const&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;::type&gt;::type myInt;\r\n  std::cout &lt;&lt; rgr::is_same&lt;<span style=\"color: #2b91af;\">int<\/span>,myInt&gt;::value &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n  \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I implemented <span style=\"font-family: courier new,courier;\">is_same<\/span> and <span style=\"font-family: courier new,courier;\">remove_const<\/span> in the namespace <span style=\"font-family: courier new,courier;\">rgr.<\/span> This corresponds to the type-traits library. For simplicity reasons, I use the static constants <span style=\"font-family: courier new,courier;\">std::false_type<\/span> and <span style=\"font-family: courier new,courier;\">std::true_type<\/span> (lines 10 and 13). I presented them in the post <a href=\"https:\/\/www.modernescpp.com\/index.php\/check-types\">Check types<\/a>. Thanks to the base class <span style=\"font-family: courier new,courier;\">std::false_type<\/span>, the class template has a member <span style=\"font-family: courier new,courier;\">value, respectively<\/span>, for <span style=\"font-family: courier new,courier;\">std::true_type.<\/span> The critical observation of the class template <span style=\"font-family: courier new,courier;\">is_same<\/span> is to distinguish the general template (lines 9 and 10) from the partially specialized template (lines 12 and 13. The compiler will use the partially specialized template if both template arguments have the same type. The partially specialized template has, in opposite to the general template, only one type parameter. My reasoning for the class template <span style=\"font-family: courier new,courier;\">remove_const<\/span> is similar. The general template returns via its member type precisely the same type; the partially specialized template returns the new type after removing the const property (line 22). The compiler will choose the partially specialized template if its template argument is const.&nbsp;<\/p>\n<p>The rest is quickly explained. I use in lines 31 and 32 the functions of the type-traits library and my versions. I declare in line 34 a typedef <span style=\"font-family: courier new,courier;\">mydouble,<\/span> a type <span style=\"font-family: courier new,courier;\">myString<\/span> (line 37), and a type <span style=\"font-family: courier new,courier;\">myInt.<\/span> All types are non-constant.<\/p>\n<p>Here is the output of the program.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4992\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/removeConst.png\" alt=\"removeConst\" width=\"503\" height=\"237\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/removeConst.png 503w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/removeConst-300x141.png 300w\" sizes=\"auto, (max-width: 503px) 100vw, 503px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>I intentionally ignored the import capability of the type-traits library. In the first step, the compiler can analyze your types at compile time and perform powerful optimizations in the second step. The result is you have a faster program. Due to the type-traits library, this is happening in various standard template library algorithms. As far as I know, all implementations of the STL use this technique. I will write in a further post about this automatic optimization. But in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/user-defined-literals\">next post<\/a>, I present user-defined literals. This is my favorite feature in modern C++ if you write safety-critical software. User-defined literals empower you to calculate with units. The compiler takes care that you don&#8217;t compare apples and pears.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div id=\"simple-translate\">\n<div>\n<div class=\"simple-translate-button isShow\" style=\"background-image: url('moz-extension:\/\/981aa874-2db4-44d3-a97f-b02a72476831\/icons\/512.png'); height: 22px; width: 22px; top: 246px; left: 44px;\">&nbsp;<\/div>\n<div class=\"simple-translate-panel\" style=\"width: 300px; height: 200px; top: 0px; left: 0px; font-size: 13px; background-color: #ffffff;\">\n<div class=\"simple-translate-result-wrapper\" style=\"overflow: hidden;\">\n<div class=\"simple-translate-move\" draggable=\"draggable\">&nbsp;<\/div>\n<div class=\"simple-translate-result-contents\">\n<p class=\"simple-translate-result\" dir=\"auto\" style=\"color: #000000;\">&nbsp;<\/p>\n<p class=\"simple-translate-candidate\" dir=\"auto\" style=\"color: #737373;\">&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The type-traits library empowers you to compare and modify types. All is done at compile time therefore, there is no performance penalty.<\/p>\n","protected":false},"author":21,"featured_media":4990,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[419],"class_list":["post-4993","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-type-traits"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4993","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=4993"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4993\/revisions"}],"predecessor-version":[{"id":6934,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4993\/revisions\/6934"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4990"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}