public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103543] New: Potential compiler warning for return of temporary?
@ 2021-12-03 16:10 gcc at hazlewoods dot net
  2021-12-03 19:44 ` [Bug c++/103543] Potential compiler warning for return of temporary with std::function<const T&()> converting from T(*)() pinskia at gcc dot gnu.org
  2021-12-03 19:45 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: gcc at hazlewoods dot net @ 2021-12-03 16:10 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103543
           Summary: Potential compiler warning for return of temporary?
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at hazlewoods dot net
  Target Milestone: ---

A return of a temporary from a lambda assigned to `std::function<>` does not
cause a warning, unlike a regular function.
Would be useful if it trapped, but it may be too far down the deduction path.

<code>
#include <iostream>
#include <functional>

struct S
{
  int s{};
};

using F = std::function<S const& ()>;

S const& fn()
{
  return S{};  // Warning: returning ref to temporary
}

int main()
{
  F f = [](){ return S{}; };  // No warning
  std::cout << f().s << std::endl;  // Ouch
}
</code>


g++-11 -W -Wall -std=c++20 -O2 -pedantic-errors test.cpp --save-temps

g++-11 --version
g++-11 (GCC) 11.2.1 20211105

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

* [Bug c++/103543] Potential compiler warning for return of temporary with std::function<const T&()> converting from T(*)()
  2021-12-03 16:10 [Bug c++/103543] New: Potential compiler warning for return of temporary? gcc at hazlewoods dot net
@ 2021-12-03 19:44 ` pinskia at gcc dot gnu.org
  2021-12-03 19:45 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-03 19:44 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
            Summary|Potential compiler warning  |Potential compiler warning
                   |for return of temporary?    |for return of temporary
                   |                            |with std::function<const
                   |                            |T&()> converting from
                   |                            |T(*)()

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
That is because the lambda itself returns a S and not a const reference.

You do get a warning with -Wsystem-headers inside invoke.

With:
[]() -> S const&  { return S{}; };

You do get the correct warning.

Also this has nothing to do with lambdas really but really std::function as
shown by:
struct S
{
  int s{};
};
using F = std::function<S const& ()>;
auto fn()
{
  return S{};
}
int main()
{
  F f = fn; // No warning
  __builtin_printf("%d.\n", f().s);
}
---- CUT ----
The above code is the same as the lambda case really.

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

* [Bug c++/103543] Potential compiler warning for return of temporary with std::function<const T&()> converting from T(*)()
  2021-12-03 16:10 [Bug c++/103543] New: Potential compiler warning for return of temporary? gcc at hazlewoods dot net
  2021-12-03 19:44 ` [Bug c++/103543] Potential compiler warning for return of temporary with std::function<const T&()> converting from T(*)() pinskia at gcc dot gnu.org
@ 2021-12-03 19:45 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-03 19:45 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is a dup of bug 70692.

*** This bug has been marked as a duplicate of bug 70692 ***

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

end of thread, other threads:[~2021-12-03 19:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-03 16:10 [Bug c++/103543] New: Potential compiler warning for return of temporary? gcc at hazlewoods dot net
2021-12-03 19:44 ` [Bug c++/103543] Potential compiler warning for return of temporary with std::function<const T&()> converting from T(*)() pinskia at gcc dot gnu.org
2021-12-03 19:45 ` pinskia at gcc dot gnu.org

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