Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constexpr references and constant expressions

Reply
Thread Tools

constexpr references and constant expressions

 
 
Inconnu
Guest
Posts: n/a
 
      11-16-2011
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;
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      11-16-2011
On Nov 16, 7:15*am, Inconnu <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: constexpr static member function Victor Bazarov C++ 1 03-02-2013 01:42 PM
Re: constexpr static member function Victor Bazarov C++ 0 03-01-2013 05:02 PM
constexpr constructor: A bug in GCC 4.5.0 Saeed Amrollahi C++ 3 03-12-2011 03:56 PM
initializer_list members not being constexpr anymore Rodolfo Lima C++ 0 06-28-2010 12:37 AM
What is the purpose of C++0X constexpr? jason.cipriani@gmail.com C++ 5 05-03-2008 09:31 AM



Advertisments