https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101998

            Bug ID: 101998
           Summary: false positive: taking address of rvalue
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: peciva at fit dot vut.cz
  Target Milestone: ---

Following code refuses to compile (on g++ 7.5 and 11.2):

struct Nested {
    int payload;
};

struct Main {
    const Nested* nested;
    int payload;
    Main(const Nested* n) : nested(n) {}
};

void f(const Main& s)
{
}

int main() {
    f(Main(&(const Nested&)Nested()));  // <- workaround
    f(Main(&Nested()));  // <- fails here
    return 0; 
}


Error message: "error: taking address of rvalue"

I am not c++ expert, but the code seems perfectly meaningful to me. Structure
Main and Nested are both constructed before the call to f and both of them are
destructed after the execution of f. No dangling pointer to destructed
temporary. Both structures (Main, Nested) are temporaries and I do not see a
real reason why the compilation should fail. Maybe, if Main and Nested
instances are both temporaries, the error should not be emitted and compilation
shall proceed (?). But I am not expert.

Such constructions of temporary nested structures and passing pointers to main
structure constructor are often used when programming with c++ Vulkan API, for
instance.

Reply via email to