constexpr references and constant expressions

Discussion in 'C++' started by Inconnu, Nov 16, 2011.

  1. Inconnu

    Inconnu Guest

    Is the following a valid code?

    constexpr const A &ri = 3;
    constexpr const int *pi = &ri;

    ------------------------------------
    Code may be more complicated:

    struct A
    {
    int i;
    const int &ri;
    };

    constexpr const A &ra = {1, 3};
    constexpr const int *pi = &ra.ri;
    --------------------------------------

    and even:

    struct A
    {
    int i;
    const int &ri;
    };

    struct B
    {
    int i;
    const A &ra;
    };

    constexpr const A &rb = {1, {1, 3}};
    constexpr const int *pi = &rb.ra.ri;
    Inconnu, Nov 16, 2011
    #1
    1. Advertising

  2. Inconnu

    Noah Roberts Guest

    On Nov 16, 7:15 am, Inconnu <> wrote:
    > Is the following a valid code?
    >
    > constexpr const A &ri = 3;
    > constexpr const int *pi = &ri;
    >
    > ------------------------------------
    > Code may be more complicated:
    >
    > struct A
    > {
    >     int i;
    >     const int &ri;
    >
    > };
    >
    > constexpr const A &ra = {1, 3};
    > constexpr const int *pi = &ra.ri;
    > --------------------------------------
    >
    > and even:
    >
    > struct A
    > {
    >     int i;
    >     const int &ri;
    >
    > };
    >
    > struct B
    > {
    >     int i;
    >     const A &ra;
    >
    > };
    >
    > constexpr const A &rb = {1, {1, 3}};
    > constexpr const int *pi = &rb.ra.ri;


    I don't have the standard, haven't made use of constexpr, so I could
    be quite wrong....but I don't think so. It wouldn't make sense to
    me. A constant expression in C++ is an expression that can be
    evaluated at compile time. The 'constexpr' statement is meant to
    create named constant expressions with variable and function syntax.
    What you're doing above though is trying to create a runtime
    reference. Just because that reference is constant (and they all are
    really) doesn't mean it's a compile-time entity.

    I do know that a constexpr is not allowed to make use of anything but
    constant expressions. If references aren't allowed, and I seriously
    doubt they are, then you can't make a constexpr of a type that
    contains one.

    Of course, you could always try it with various compilers to get a
    rough idea of whether it's allowed or not. Not definitive, but it is
    practical.
    Noah Roberts, Nov 16, 2011
    #2
    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. Replies:
    5
    Views:
    499
    Erik Wikström
    May 3, 2008
  2. Rodolfo Lima
    Replies:
    0
    Views:
    288
    Rodolfo Lima
    Jun 28, 2010
  3. Saeed Amrollahi
    Replies:
    3
    Views:
    1,099
  4. Victor Bazarov

    Re: constexpr static member function

    Victor Bazarov, Mar 1, 2013, in forum: C++
    Replies:
    0
    Views:
    248
    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