{"id":6664,"date":"2015-04-29T11:41:00","date_gmt":"2015-04-29T11:41:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/04\/29\/c11-constant-expressions-in-visual-studio-2015-rc\/"},"modified":"2019-02-18T18:05:05","modified_gmt":"2019-02-18T18:05:05","slug":"c11-constant-expressions-in-visual-studio-2015-rc","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/c11-constant-expressions-in-visual-studio-2015-rc\/","title":{"rendered":"C++11 Constant Expressions in Visual Studio 2015 RC"},"content":{"rendered":"<p>In <a href=\"http:\/\/blogs.msdn.com\/b\/vcblog\/archive\/2015\/04\/29\/visual-studio-2015-rc-now-available.aspx\">Visual Studio 2015 RC<\/a>, we have implemented <a href=\"http:\/\/www.stroustrup.com\/sac10-constexpr.pdf\">C++11&rsquo;s Constant Expressions<\/a>, a feature that has been in development since the <a href=\"http:\/\/blogs.msdn.com\/b\/vcblog\/archive\/2013\/11\/18\/announcing-the-visual-c-compiler-november-2013-ctp.aspx\">November 2013 CTP<\/a>. This article will describe some neat things that you can do with it, the differences in capabilities between this feature&rsquo;s current and older implementations, and our future plans for it.<\/p>\n<h2>What Are Constant Expressions?<\/h2>\n<p>The Constant Expressions feature allows you to create, using the <span style=\"font-family: Consolas\">constexpr<\/span> keyword, computations that can run during compilation and whose results can be considered <span style=\"font-family: Consolas\">const<\/span>. Something can only be declared <span style=\"font-family: Consolas\">constexpr<\/span> if it satisfies all necessary constraints that allow it to be evaluated by the compiler using only the information available at compile time.<\/p>\n<h2>What Can They Be Used For?<\/h2>\n<h2>Type Safety<\/h2>\n<p>Prior to the introduction of constant expressions, it was common to use macros to compute <span style=\"font-family: Consolas\">const<\/span> values:\n&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">#define&nbsp;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #6f008a;background: white\">LENGTHOF<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">(x) (<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">sizeof<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">(x) \/ <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">sizeof<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">(x[0]))<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">char<\/span><\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">x[10];<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: blue;background: white\">char<\/span> <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">y[<\/span><span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: #6f008a;background: white\">LENGTHOF<\/span><span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">(x)];&nbsp;<\/span><span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: green;background: white\">\/\/ y also has 10 elements<\/span>\nBut macros are not type safe. The following code compiles despite the fact that <span style=\"font-family: Consolas\">x<\/span> is a pointer, which results in the calculation of nonsense:\n&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">char<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">*x;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: blue;background: white\">char<\/span> <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">y[<\/span><span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: #6f008a;background: white\">LENGTHOF<\/span><span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">(x)];<\/span> <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: green;background: white\">\/\/ No error! Expands to (sizeof(char *) \/ sizeof(x[0]))<\/span>\nTo avoid this issue, we can use constant expressions:\n&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">template<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;<\/span><span class=\"SpellE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">typename <\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">T<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">size_t <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">length&gt;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span class=\"SpellE\"><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">constexpr <\/span><\/span><\/span><span class=\"SpellE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">size_t<\/span><\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">lengthof(<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">T <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">(&amp;)[length])<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;return <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">length;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">}<\/span>\nThis function works just like the <span style=\"font-size: 9.5pt;font-family: Consolas;color: #6f008a;background: white\">LENGTHOF<\/span> macro:\n&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">char <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">x[10];<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: blue;background: white\">char <\/span><span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">y[lengthof(x)];<\/span>\nExcept that the compiler emits an error if <span style=\"font-family: Consolas\">x<\/span> is not an array, which prevents this function from being used in ways that don&#8217;t make sense:\n&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">char <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">*x;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: blue;background: white\">char <\/span><span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">y[lengthof(x)]; <\/span><span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: green;background: white\">\/\/ Error C2784: &#8216;size_t lengthof(T (&amp;)[N])&#8217;: could not deduce template argument for &#8216;T (&amp;)[N]&#8217; from &#8216;char *&#8217;<\/span><\/p>\n<p class=\"MsoNormal\">Generally speaking, you should use constant expressions instead of macros wherever you can, because the former lets the compiler perform the same type checking that it does for normal C++ code.<\/p>\n<h2>Meta-Programming<\/h2>\n<p>C++&#8217;s template system is actually a full-fledged functional programming language, and as such, it is often used to perform complex compile-time computations. But because it was not designed as a general purpose language, it is usually cumbersome and sometimes difficult to express these computations.\nFor example, if for some reason you need to compute exponentiation at the type level, you&#8217;d have to write something like:\n&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">#include <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #a31515;background: white\">&lt;type_traits&gt;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">template <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">x, <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">n, <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">typename <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">Cond <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">= <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">void<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&gt;<\/span> <\/p>\n<p>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">struct <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">Exp<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;static <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">const <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">result =<\/span> <span class=\"SpellE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">Exp<\/span><\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;x * x, n \/ 2&gt;::result;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">};<\/span> <\/p>\n<p>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">template<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">x, <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">n&gt;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">struct <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">Exp<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;x, n, std::<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">enable_if_t<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;n % 2 == 1&gt;&gt;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;static <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">const <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">result = <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">Exp<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;x * x, (n &#8211; 1) \/ 2&gt;::result * x;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">};<\/span> <\/p>\n<p>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">template<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">x&gt;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">struct <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">Exp<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&lt;x, 0&gt;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;static <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">const <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">result = 1;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">};<\/span>\nStrange syntax, spread out cases, and roundabout condition checking hinder one&#8217;s understanding of this code, which makes debugging it difficult. Also, since floating point numbers can&#8217;t be used as non-type template parameters, this exponentiation &#8220;function&#8221; will only work for integers, which is a serious limitation. To improve readability and functionality, constant expressions can be used instead:\n&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">constexpr <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">float <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">exp(<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">float<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">x<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">,<\/span> <span class=\"SpellE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int<\/span><\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">n<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">)<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;return <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">n <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">== 0 ? &nbsp;&nbsp;&nbsp;&nbsp;1 :<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">% 2 == 0 ? exp(<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">x <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">* <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">x<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">n <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">\/ 2) :<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exp(<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">x <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">* <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">x<\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">, (<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">n <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">&#8211; 1) \/ 2) * <\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: gray;background: white\">x<\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">;<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">};<\/span>\nBecause we are now using normal C++ expressions, this function is not only much shorter and easier to read, but also capable of handling floating point arguments.<\/p>\n<h2>Known Issues<\/h2>\n<p>Currently, implicitly-defined constructors aren&#8217;t automatically specified as <span style=\"font-family: Consolas\">constexpr<\/span> even when they would fulfill all the requirements to be so:\n&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">struct <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">A<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;virtual <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">void <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">f();<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">};<\/span> <\/p>\n<p>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">struct <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">B <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">: <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">A<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;constexpr <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">B() {}; <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: green;background: white\">\/\/ error C2134 : &#8216;A::A&#8217; : call does not result in a constant expression<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">};<\/span><\/p>\n<p class=\"MsoNormal\">To work around this, you need to explicitly define a <span class=\"SpellE\"><span style=\"font-family: Consolas\">constexpr<\/span><\/span> constructor for that class:\n&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">struct <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">A<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;virtual <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">void <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">f();<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;constexpr <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">A() {};<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">};<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas\">constexpr<\/span> constructors also can&#8217;t yet initialize array data members:\n&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">struct <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">S <\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;int <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">a[5]; <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: green;background: white\">\/\/ note: &#8216;S::a&#8217; was not initialized by the constructor<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;constexpr <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">S() : a() { } <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: green;background: white\">\/\/ error C2476: &#8216;constexpr&#8217; constructor does not initialize all members<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">};<\/span> <\/p>\n<p>&emsp;&emsp;&emsp;&emsp; <span class=\"SpellE\"><span class=\"GramE\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">int<\/span><\/span><\/span> <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">main()<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">{<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;font-family: Consolas;color: blue;background: white\">&nbsp;&nbsp;&nbsp;&nbsp;constexpr <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: #2b91af;background: white\">S <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: black;background: white\">b; <\/span><span style=\"font-size: 9.5pt;font-family: Consolas;color: green;background: white\">\/\/ error C2127: &#8216;b&#8217;: illegal initialization of &#8216;constexpr&#8217; entity with a non-constant expression<\/span> <br \/>&emsp;&emsp;&emsp;&emsp; <span style=\"font-size: 9.5pt;line-height: 107%;font-family: Consolas;color: black;background: white\">}<\/span>\nWe plan to fix these bugs, among others, in Visual Studio 2015 RTM or Update 1.<\/p>\n<h2>Next Steps<\/h2>\n<p>C++14&#8217;s &#8220;generalized&#8221; constant expressions feature relaxes some restrictions with the constructs described above. For example, in C++14, <span class=\"SpellE\"><span style=\"font-family: Consolas\">constexpr<\/span><\/span> functions can contain statements, which will allow them to make use of loops and modify local variables. We plan to implement C++14 constant expressions in the <a href=\"http:\/\/blogs.msdn.com\">near future<span style=\"font-size: 10.6666669845581px;line-height: 11.4133338928223px\">.<\/span><\/a><\/p>\n<p>Since we are always looking for ways to improve our compiler, please make heavy use of this feature in your code and keep sending us feedback through <a href=\"https:\/\/connect.microsoft.com\/\">Microsoft Connect<\/a> or the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/hh362461.aspx\">Visual Studio feedback tool<\/a>. You can also contact me directly at <a href=\"http:\/\/blogs.msdn.commailto:kaniu@microsoft.com\">kaniu@microsoft.com<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Visual Studio 2015 RC, we have implemented C++11&rsquo;s Constant Expressions, a feature that has been in development since the November 2013 CTP. This article will describe some neat things that you can do with it, the differences in capabilities between this feature&rsquo;s current and older implementations, and our future plans for it. What Are [&hellip;]<\/p>\n","protected":false},"author":299,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[137],"class_list":["post-6664","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-announcements"],"acf":[],"blog_post_summary":"<p>In Visual Studio 2015 RC, we have implemented C++11&rsquo;s Constant Expressions, a feature that has been in development since the November 2013 CTP. This article will describe some neat things that you can do with it, the differences in capabilities between this feature&rsquo;s current and older implementations, and our future plans for it. What Are [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/6664","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\/299"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=6664"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/6664\/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=6664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=6664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=6664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}