Using 'this' in a constexpr constructor

A

a.s.matveyev

Say, for example, that I've got an object with static storage duration, andthe constructor of this object (declared as constexpr) sets one of the members to this. Will such an object be initialised statically? I can't find any clear answer to this question in the standard. On the one hand, using 'this' doesn't work very well with constant expressions, as you can see in 5.19, but on the other hand taking the address of an object with static storage duration produces a valid constant expression (5.19 again) and I can't see why 'this' should be different. I've made a simple example (http://ideone.com/IIe0Ay) to try and see what different compilers think about this matter, and it turned out that even compilers disagree with each other here. The binary produced by clang works nice and well (static initialisation, I guess), while the one produced by gcc segfaults immediately (trying to initialise root dynamically). Basically, my question is what the standard really says on the matter and why?
 
J

James Kanze

Say, for example, that I've got an object with static storage
duration, and the constructor of this object (declared as
constexpr) sets one of the members to this. Will such an
object be initialised statically?

If the object has a non-trivial constructor, it cannot be
initialized statically.
 
A

a.s.matveyev

If the object has a non-trivial constructor, it cannot be

initialized statically.

3.6.2
"Constant initialization is performed:
....
-- if an object with static or thread storage duration is initialized by a constructor call, if the constructor is a constexpr constructor, if all constructor arguments are constant expressions (including conversions), and if, after function invocation substitution (7.1.5), every constructor call and full-expression in the mem-initializers is a constant expression"

Welcome to the magic world of C++11
 
Ö

Öö Tiib

3.6.2
"Constant initialization is performed:
...
-- if an object with static or thread storage duration is initialized by a constructor call, if the constructor is a constexpr constructor, if all constructor arguments are constant expressions (including conversions), and if, after function invocation substitution (7.1.5), every constructor call and full-expression in the mem-initializers is a constant expression"

Welcome to the magic world of C++11

You used 'this' in mem-initializer. I can't find any cases where standard requires
keyword 'this' to return a constant expression. Either I searched badly or keyword
'this' is not required to be constant expression in 'constexpr' members and
constructors.
 
A

a.s.matveyev

You used 'this' in mem-initializer. I can't find any cases where standardrequires

keyword 'this' to return a constant expression. Either I searched badly or keyword

'this' is not required to be constant expression in 'constexpr' members and

constructors.
In general it isn't, of course, but I can't see why it cannot be regarded as a constant expression if the object in question has static storage duration. Taking the address of such an object *is* a constant expression, after all. Well, I guess nothing is perfect, including the C++ standard
 
Ö

Öö Tiib

In general it isn't, of course, but I can't see why it cannot be regardedas a constant
expression if the object in question has static storage duration. Taking the address
of such an object *is* a constant expression, after all. Well, I guess nothing is perfect,
including the C++ standard

It might be. I also don't see good reason. However everything can be made more
perfect. Lot of people concerned of making C++ standard more perfect are
discussing it in isocpp.org groups.
https://groups.google.com/a/isocpp.org/forum/#!forum/std-discussion
You can try to post there and maybe get answer why it is so, if it is a defect
and if it is already fired as defect report.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top