Using 'this' in a constexpr constructor

Discussion in 'C++' started by a.s.matveyev, Apr 15, 2014.

  1. a.s.matveyev

    a.s.matveyev Guest

    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?
     
    a.s.matveyev, Apr 15, 2014
    #1
    1. Advertisements

  2. a.s.matveyev

    James Kanze Guest

    If the object has a non-trivial constructor, it cannot be
    initialized statically.
     
    James Kanze, Apr 18, 2014
    #2
    1. Advertisements

  3. a.s.matveyev

    a.s.matveyev Guest

    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
     
    a.s.matveyev, Apr 23, 2014
    #3
  4. a.s.matveyev

    Öö Tiib Guest

    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.
     
    Öö Tiib, Apr 23, 2014
    #4
  5. a.s.matveyev

    a.s.matveyev Guest

    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
     
    a.s.matveyev, Apr 24, 2014
    #5
  6. a.s.matveyev

    Öö Tiib Guest

    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.
     
    Öö Tiib, Apr 25, 2014
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.