constexpr

I've been learning about constants and I can't really get the key difference between const and constexpr. It said something about "evaluated at compile time" but that makes no sense to me.
const's primary function is to express the idea that an object is not modified through an interface (even though the object may very well be modified through other interfaces) - http://www.stroustrup.com/C++11FAQ.html#constexpr


1
2
3
4
5
6
7
8
9
struct A { /* ..... */ } ;

void foo( const A& c ) ; // foo can't modify the object through c

void bar( A& a ) // bar can modify the object through a
{
      // modify a
      foo(a) ; // foo can't modify the object 
}


In addition to be able to evaluate expressions at compile time, we want to be able to require expressions to be evaluated at compile time; constexpr in front of a variable definition does that (and implies const):
...
constexpr's primary function is to extend the range of what can be computed at compile time, making such computation type safe. - Stroustrup


Also see: http://en.wikipedia.org/wiki/C%2B%2B11#constexpr_.E2.80.93_Generalized_constant_expressions
Topic archived. No new replies allowed.