Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   What is the purpose of C++0X constexpr? (http://www.velocityreviews.com/forums/t610897-what-is-the-purpose-of-c-0x-constexpr.html)

 jason.cipriani@gmail.com 05-02-2008 05:27 PM

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

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

http://en.wikipedia.org/wiki/C%2B%2B...nt_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()];

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

Thanks,
Jason

 Joe Greer 05-02-2008 06:09 PM

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

"jason.cipriani@gmail.com" <jason.cipriani@gmail.com> wrote in

> I am reading the description of "generalized constant expressions" in C
> ++0x here:
>
> http://en.wikipedia.org/wiki/C%2B%2B...nt_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()];
>
>
> 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

 jason.cipriani@gmail.com 05-02-2008 08:40 PM

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

On May 2, 2:09 pm, Joe Greer <jgr...@doubletake.com> wrote:
> 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

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

 Jim Langston 05-02-2008 11:12 PM

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

jason.cipriani@gmail.com wrote:
> I am reading the description of "generalized constant expressions" in
> C ++0x here:
>
> http://en.wikipedia.org/wiki/C%2B%2B...nt_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()];
>
>
> 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.

--
Jim Langston
tazmaster@rocketmail.com

 jason.cipriani@gmail.com 05-03-2008 02:27 AM

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

On May 2, 4:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> 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[B];

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

 Erik Wikström 05-03-2008 09:31 AM

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

On 2008-05-03 04:27, jason.cipriani@gmail.com wrote:
> On May 2, 4:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> 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[B];
>
> 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.