C++11 Constant Expressions in Visual Studio 2015 RC

April 29th, 2015

In Visual Studio 2015 RC, we have implemented C++11’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’s current and older implementations, and our future plans for it.

What Are Constant Expressions?

The Constant Expressions feature allows you to create, using the constexpr keyword, computations that can run during compilation and whose results can be considered const. Something can only be declared constexpr if it satisfies all necessary constraints that allow it to be evaluated by the compiler using only the information available at compile time.

What Can They Be Used For?

Type Safety

Prior to the introduction of constant expressions, it was common to use macros to compute const values:#define LENGTHOF(x) (sizeof(x) / sizeof(x[0]))charx[10];chary[LENGTHOF(x)]; // y also has 10 elements But macros are not type safe. The following code compiles despite the fact that x is a pointer, which results in the calculation of nonsense:char*x;chary[LENGTHOF(x)];// No error! Expands to (sizeof(char *) / sizeof(x[0])) To avoid this issue, we can use constant expressions:template<typename T, size_t length>constexpr size_tlengthof(T (&)[length]){ return length;} This function works just like the LENGTHOF macro:char x[10];char y[lengthof(x)]; Except that the compiler emits an error if x is not an array, which prevents this function from being used in ways that don’t make sense:char *x;char y[lengthof(x)]; // Error C2784: ‘size_t lengthof(T (&)[N])’: could not deduce template argument for ‘T (&)[N]’ from ‘char *’

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.

Meta-Programming

C++’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. For example, if for some reason you need to compute exponentiation at the type level, you’d have to write something like:#include <type_traits>template <int x, int n, typename Cond = void>

template<int x>struct Exp<x, 0>{ static const int result = 1;}; Strange syntax, spread out cases, and roundabout condition checking hinder one’s understanding of this code, which makes debugging it difficult. Also, since floating point numbers can’t be used as non-type template parameters, this exponentiation “function” will only work for integers, which is a serious limitation. To improve readability and functionality, constant expressions can be used instead:constexpr float exp(floatx,intn){ return n == 0 ? 1 : n % 2 == 0 ? exp(x * x, n / 2) : exp(x * x, (n – 1) / 2) * x;}; Because 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.

Known Issues

Currently, implicitly-defined constructors aren’t automatically specified as constexpr even when they would fulfill all the requirements to be so:struct A{ virtual void f();};

Next Steps

C++14’s “generalized” constant expressions feature relaxes some restrictions with the constructs described above. For example, in C++14, constexpr 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 near future.