public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101998] New: false positive: taking address of rvalue
@ 2021-08-20 14:35 peciva at fit dot vut.cz
  2021-08-20 14:40 ` [Bug c++/101998] " redi at gcc dot gnu.org
  2021-08-21  6:14 ` peciva at fit dot vut.cz
  0 siblings, 2 replies; 3+ messages in thread
From: peciva at fit dot vut.cz @ 2021-08-20 14:35 UTC (permalink / raw)
  To: gcc-bugs

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.

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

* [Bug c++/101998] false positive: taking address of rvalue
  2021-08-20 14:35 [Bug c++/101998] New: false positive: taking address of rvalue peciva at fit dot vut.cz
@ 2021-08-20 14:40 ` redi at gcc dot gnu.org
  2021-08-21  6:14 ` peciva at fit dot vut.cz
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-20 14:40 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The C++ standard forbids it, that's why GCC gives an error.

"The operand of the unary & operator shall be an lvalue of some type T."

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

* [Bug c++/101998] false positive: taking address of rvalue
  2021-08-20 14:35 [Bug c++/101998] New: false positive: taking address of rvalue peciva at fit dot vut.cz
  2021-08-20 14:40 ` [Bug c++/101998] " redi at gcc dot gnu.org
@ 2021-08-21  6:14 ` peciva at fit dot vut.cz
  1 sibling, 0 replies; 3+ messages in thread
From: peciva at fit dot vut.cz @ 2021-08-21  6:14 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jan Pečiva <peciva at fit dot vut.cz> ---
I see. Thanks!

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

end of thread, other threads:[~2021-08-21  6:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-20 14:35 [Bug c++/101998] New: false positive: taking address of rvalue peciva at fit dot vut.cz
2021-08-20 14:40 ` [Bug c++/101998] " redi at gcc dot gnu.org
2021-08-21  6:14 ` peciva at fit dot vut.cz

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).