public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/54293] New: When a reference is bound to subobject of a temporary, lifetime of the temporary is not extended
@ 2012-08-16 22:28 ppluzhnikov at google dot com
  2012-08-16 23:02 ` [Bug c++/54293] " pinskia at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: ppluzhnikov at google dot com @ 2012-08-16 22:28 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54293

             Bug #: 54293
           Summary: When a reference is bound to subobject of a temporary,
                    lifetime of the temporary is not extended
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: ppluzhnikov@google.com


Google reference: b/6996555

"ISO/IEC 14882:2011(E) 12.2.5 [class.temporary]

The temporary to which the reference is bound or the temporary that is the
complete object of a subobject to which the reference is bound persists for the
lifetime of the reference except: [etc]"

With gcc-4.6, the lifetime extension is not happening at all:

FAIL int
FAIL Obj

With gcc-4.7 and 4.8 (rev 190453), lifetime is extended for Obj subobject, but
not for 'int' (or 'char', or other primitive types):

FAIL int

The test:

#include <set>
#include <iostream>

std::set<const void*> subobjs;

template <typename T>
struct ValueHolder {
  explicit ValueHolder() {
    subobjs.insert(&v);
  }
  ~ValueHolder() {
    subobjs.erase(&v);
  }
  T v;
};

struct Obj { };

bool IsValid(const void* p) {
  return subobjs.find(p) != subobjs.end();
}

int main() {
  const int& ref_int = ValueHolder<int>().v;
  if (!IsValid(&ref_int)) {
    std::cout << "FAIL int" << std::endl;
  }

  const Obj& ref_obj = ValueHolder<Obj>().v;
  if (!IsValid(&ref_obj)) {
    std::cout << "FAIL Obj" << std::endl;
  }
}


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-08-21  8:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-16 22:28 [Bug c++/54293] New: When a reference is bound to subobject of a temporary, lifetime of the temporary is not extended ppluzhnikov at google dot com
2012-08-16 23:02 ` [Bug c++/54293] " pinskia at gcc dot gnu.org
2012-08-16 23:07 ` ppluzhnikov at google dot com
2012-08-17 14:19 ` aaw at gcc dot gnu.org
2012-08-17 14:55 ` jpalecek at web dot de
2012-08-20 18:14 ` crowl at gcc dot gnu.org
2012-08-20 21:56 ` jpalecek at web dot de
2012-08-20 22:18 ` daniel.kruegler at googlemail dot com
2012-08-20 22:52 ` jpalecek at web dot de
2012-08-21  6:14 ` daniel.kruegler at googlemail dot com
2012-08-21  7:52 ` jpalecek at web dot de
2012-08-21  8:08 ` daniel.kruegler at googlemail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).