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

Discussion in 'C++' started by jason.cipriani@gmail.com, May 2, 2008.

  1. Guest

    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
    , May 2, 2008
    #1
    1. Advertising

  2. Joe Greer Guest

    "" <> wrote in
    news::

    > 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
    Joe Greer, May 2, 2008
    #2
    1. Advertising

  3. Guest

    On May 2, 2:09 pm, Joe Greer <> 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
    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
    , May 2, 2008
    #3
  4. Jim Langston Guest

    wrote:
    > 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.


    --
    Jim Langston
    Jim Langston, May 3, 2008
    #4
  5. Guest

    On May 2, 4:57 pm, "Victor Bazarov" <> 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;

    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
    , May 3, 2008
    #5
  6. On 2008-05-03 04:27, wrote:
    > On May 2, 4:57 pm, "Victor Bazarov" <> 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;
    >
    > 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

    --
    Erik Wikström
    Erik Wikström, May 3, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rodolfo Lima
    Replies:
    0
    Views:
    288
    Rodolfo Lima
    Jun 28, 2010
  2. Saeed Amrollahi
    Replies:
    3
    Views:
    1,093
  3. Inconnu
    Replies:
    1
    Views:
    424
    Noah Roberts
    Nov 16, 2011
  4. Victor Bazarov

    Re: constexpr static member function

    Victor Bazarov, Mar 1, 2013, in forum: C++
    Replies:
    0
    Views:
    245
    Victor Bazarov
    Mar 1, 2013
  5. Victor Bazarov

    Re: constexpr static member function

    Victor Bazarov, Mar 1, 2013, in forum: C++
    Replies:
    1
    Views:
    209
    Victor Bazarov
    Mar 2, 2013
Loading...

Share This Page