Bug 114675 - warning for "reference to not fully constructed object"
Summary: warning for "reference to not fully constructed object"
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 13.1.1
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wuninitialized new-warning, new_warning
  Show dependency treegraph
 
Reported: 2024-04-10 09:15 UTC by Simon Richter
Modified: 2024-04-10 21:44 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-04-10 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Richter 2024-04-10 09:15:47 UTC
We have an object that contains several sub-objects that should reference each other through interface pointers. I'd like to make sure that interface pointers given to objects constructed later already point to initialized objects.

So, for

struct everything {
    everything() : a(), b(c), c(a) { }

    A a;
    B b;
    C c;
};

I'm passing a reference to the not-yet-constructed C object to B's constructor, so this is unsafe despite being legal C++. I wonder if it would be possible to generate a warning here.
Comment 1 Jonathan Wakely 2024-04-10 10:42:59 UTC
A complete testcase that actually compiles:

struct A { };
struct C { C(const A&); };
struct B { B(const C&); };

struct everything {
    everything() : a(), b(c), c(a) { }

    A a;
    B b;
    C c;
};
Comment 2 Andrew Pinski 2024-04-10 11:14:51 UTC
(In reply to Jonathan Wakely from comment #1)
> A complete testcase that actually compiles:
> 
> struct A { };
> struct C { C(const A&); };
> struct B { B(const C&); };
> 
> struct everything {
>     everything() : a(), b(c), c(a) { }
> 
>     A a;
>     B b;
>     C c;
> };

Confirmed. I thought I had saw another bug which was asking for the same thing but I can't seem to find it right now.
Comment 3 Jonathan Wakely 2024-04-10 12:12:13 UTC
Yeah I looked for a dup too, as I'm sure this has been reported before.