What is the purpose of C++0X constexpr?

J

jason.cipriani

I am reading the description of "generalized constant expressions" in C
++0x here:

http://en.wikipedia.org/wiki/C++0x#Generalized_constant_expressions

And I don't understand the purpose of constexpr.

How are these different?

constexpr int Number = 5;
const int Number = 5;

And when would you ever want to do this:

constexpr int Number () { return 5; }
double array[Number()];

Instead of just:

const int Number = 5;
double array[Number];

Thanks,
Jason
 
J

Joe Greer

I am reading the description of "generalized constant expressions" in C
++0x here:

http://en.wikipedia.org/wiki/C++0x#Generalized_constant_expressions

And I don't understand the purpose of constexpr.

How are these different?

constexpr int Number = 5;
const int Number = 5;

And when would you ever want to do this:

constexpr int Number () { return 5; }
double array[Number()];

Instead of just:

const int Number = 5;
double array[Number];

Thanks,
Jason

If I recall correctly (and I probably don't), the constexpr comes from the
problem caused by certain traits classes presenting some of their constants
as functions (think numeric_limits<T>::max() for example), This means that
you can't use that value to declare an array. The constexpr feature allows
you to present your constants as a function, but yet use it anyplace a
constant can be used. I think this becomes important in metaprogramming
where a list of constants from diverse places might be put together and a
value selected at compile time to be used for dimensioning. I could be
wrong as I don't generally do that sophisticated level of template
programming.

joe
 
J

jason.cipriani

If I recall correctly (and I probably don't), the constexpr comes from the
problem caused by certain traits classes presenting some of their constants
as functions (think numeric_limits<T>::max() for example), This means that
you can't use that value to declare an array.

I see. That makes sense. I was going to say it seems like some feature
creep just to handle those rare cases, but playing around with it I
guess there is no other way to, say, declare an array of size
numeric_limits<char>::max(), to use your example.

On the other hand, I still don't understand the following example
given on the wiki page I linked to:

constexpr double forceOfGravity = 9.8;

How would that be different than:

const double forceOfGravity = 9.8;

?

Thanks,
Jason
 
J

Jim Langston

I am reading the description of "generalized constant expressions" in
C ++0x here:

http://en.wikipedia.org/wiki/C++0x#Generalized_constant_expressions

And I don't understand the purpose of constexpr.

How are these different?

constexpr int Number = 5;
const int Number = 5;

And when would you ever want to do this:

constexpr int Number () { return 5; }
double array[Number()];

Instead of just:

const int Number = 5;
double array[Number];

int ArraySize()
{
return 5;
}

double Array[ArraySize()];

Illegal in C++. Even though ArraySize() returns a constant, the compiler
can't guarantee that.

constexpr int ArraySize()
{
return 5;
}

double Array[ArraySize()];

Legal in C++0x. constexpr states that the function returns a constant
expression.
 
J

jason.cipriani

There is probably the difference in the treatment of the expressions
of a floating point type. Most of FP type expressions are not
calcualated at the compile time _unless_ (and that's what the new
feature is, I am guessing) it is declared 'constexpr'...

But even with integers, both of these would be valid in C++0x, right?

const int A = 5;
int array1[A];

constexpr int B = 5;
int array2;

Is there some difference between const and constexpr when used to
modify a variable that is assigned a literal value anyways? Or are the
above two examples identical? That's the part that I don't really
understand.

Thanks!
Jason
 
E

Erik Wikström

There is probably the difference in the treatment of the expressions
of a floating point type. Most of FP type expressions are not
calcualated at the compile time _unless_ (and that's what the new
feature is, I am guessing) it is declared 'constexpr'...

But even with integers, both of these would be valid in C++0x, right?

const int A = 5;
int array1[A];

constexpr int B = 5;
int array2;

Is there some difference between const and constexpr when used to
modify a variable that is assigned a literal value anyways? Or are the
above two examples identical? That's the part that I don't really
understand.


With constexpr you can do things like this:

constexpr int square(int x)
{
return x * x;
}

float array[square(9)];

Without constexpr you can not, since the return-value of square would
have to be computed at run-time and thus the result would not be a
constant expression.

For more information and rationale see
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2235.pdf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top