{"id":1410,"date":"2015-11-12T20:48:14","date_gmt":"2015-11-12T19:48:14","guid":{"rendered":"http:\/\/netrix.org.pl\/?p=1410"},"modified":"2015-11-12T20:48:14","modified_gmt":"2015-11-12T19:48:14","slug":"variable-template-in-c14","status":"publish","type":"post","link":"https:\/\/netrix.org.pl\/index.php\/2015\/11\/12\/variable-template-in-c14\/","title":{"rendered":"Variable Template in C++14"},"content":{"rendered":"<p>C++14 introduced new type of templates which are <em>variable templates<\/em><a href=\"#footnote_0_1410\" id=\"identifier_0_1410\" class=\"footnote-link footnote-identifier-link\" title=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/variable_template\">1<\/a>. What it means is that it&#8217;s now possible to create template of a variable like this:<\/p>\n<pre lang=\"cpp\" line=\"1\">\r\ntemplate<class Type>\r\nconstexpr Type PI = (Type)3.1415;\r\n\r\nauto v = PI<double>;\r\n<\/pre>\n<p>This introduces a variable that can take different values for different template parameter. Main gain comes when such value is specialized like template:<\/p>\n<pre lang=\"cpp\" line=\"1\">\r\ntemplate<>\r\nconstexpr int PI = 4;  \/\/ Because I can\r\n\r\nauto v = PI<int>;\r\n<\/pre>\n<p>Of course in previous versions of standard this could be done in a slightly different way by using functions like this:<\/p>\n<pre lang=\"cpp\" line=\"1\">\r\ntemplate<class Type>\r\nconstexpr Type PI()\r\n{\r\n   return (Type)3.1415;\r\n}\r\n\r\ntemplate<>\r\nconstexpr int PI()\r\n{\r\n   return 4;  \/\/ Because I can\r\n}\r\n\r\nauto v = PI<double>();\r\n<\/pre>\n<p>The difference in usage is &#8220;()&#8221; which is used in a function call when using function template version. But there is also another difference which makes variable template unfortunately less useful than function template. Since C++11 it is possible to delete a specialization of function template but it seems it&#8217;s not possible to delete specialization of variable template:<\/p>\n<pre lang=\"cpp\" line=\"1\">\r\ntemplate<class Type>\r\nconstexpr Type PI()\r\n{\r\n   return (Type)3.1415;\r\n}\r\n\r\ntemplate<>\r\nconstexpr int PI() = delete;\r\n\r\ntemplate<class Type>\r\nconstexpr Type ANSWER = (Type)42;\r\n\r\n\/\/template<>\r\n\/\/constexpr double ANSWER<double> = delete;   \/\/ not possible\r\n<\/pre>\n<p>But of course no one says that we cannot use both as a syntactic sugar:<\/p>\n<pre lang=\"cpp\" line=\"1\">\r\ntemplate<class Type>\r\nconstexpr Type PI_VAR = PI<Type>();\r\n<\/pre>\n<ol class=\"footnotes\"><li id=\"footnote_0_1410\" class=\"footnote\"><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/variable_template\" title=\"Variable template\" target=\"_blank\">http:\/\/en.cppreference.com\/w\/cpp\/language\/variable_template<\/a> [<a href=\"#identifier_0_1410\" class=\"footnote-link footnote-back-link\">&#8617;<\/a>]<\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>C++14 introduced new type of templates which are variable templates1. What it means is that it&#8217;s now possible to create template of a variable like this: template constexpr Type PI = (Type)3.1415; auto v = PI; This introduces a variable that can take different values for different template parameter. Main gain comes when such value [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[179,195,194,193],"_links":{"self":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts\/1410"}],"collection":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/comments?post=1410"}],"version-history":[{"count":10,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts\/1410\/revisions"}],"predecessor-version":[{"id":1420,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/posts\/1410\/revisions\/1420"}],"wp:attachment":[{"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/media?parent=1410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/categories?post=1410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/netrix.org.pl\/index.php\/wp-json\/wp\/v2\/tags?post=1410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}