![]() |
constexpr references and constant expressions
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; |
Re: constexpr references and constant expressions
On Nov 16, 7:15*am, Inconnu <shirare...@yandex.ru> 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. |
| All times are GMT. The time now is 12:36 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.