public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96877] New: Erroneous warning when default initializing function pointer types defined using std::declval
@ 2020-09-01  2:07 insertinterestingnamehere at gmail dot com
  2020-09-01 15:29 ` [Bug c++/96877] " insertinterestingnamehere at gmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: insertinterestingnamehere at gmail dot com @ 2020-09-01  2:07 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96877
           Summary: Erroneous warning when default initializing function
                    pointer types defined using std::declval
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: insertinterestingnamehere at gmail dot com
  Target Milestone: ---

When compiling the following with -Wextra (C++11 and later, all recent version
of gcc) the compiler emits an erroneous warning about binding a temporary in a
constructor. Note: this works fine if function_type is declared as a plain
typedef instead of using decltype and std::declval. See
https://godbolt.org/z/sjKdP5.



#include <utility>

using function_type = decltype(std::declval<void (*)(void*) noexcept>());

struct S {
  function_type fptr = nullptr;
};

int main() {
    S thing;
}

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

* [Bug c++/96877] Erroneous warning when default initializing function pointer types defined using std::declval
  2020-09-01  2:07 [Bug c++/96877] New: Erroneous warning when default initializing function pointer types defined using std::declval insertinterestingnamehere at gmail dot com
@ 2020-09-01 15:29 ` insertinterestingnamehere at gmail dot com
  2020-09-01 17:37 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: insertinterestingnamehere at gmail dot com @ 2020-09-01 15:29 UTC (permalink / raw)
  To: gcc-bugs

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

Ian Henriksen <insertinterestingnamehere at gmail dot com> changed:

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

--- Comment #1 from Ian Henriksen <insertinterestingnamehere at gmail dot com> ---
Actually this is my mistake. std::declval returns by reference, so this is
actually a reference type and the compiler is right to warn about the bad
initialization. In this case the correct thing to do is to remove the reference
after getting the type from decltype.

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

* [Bug c++/96877] Erroneous warning when default initializing function pointer types defined using std::declval
  2020-09-01  2:07 [Bug c++/96877] New: Erroneous warning when default initializing function pointer types defined using std::declval insertinterestingnamehere at gmail dot com
  2020-09-01 15:29 ` [Bug c++/96877] " insertinterestingnamehere at gmail dot com
@ 2020-09-01 17:37 ` redi at gcc dot gnu.org
  2020-09-01 19:19 ` insertinterestingnamehere at gmail dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-09-01 17:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Right. But I don't understand why you'd ever want decltype(std::declval<T>())
because the result is always going to be T&& so if you know T, then you already
know the decltype.

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

* [Bug c++/96877] Erroneous warning when default initializing function pointer types defined using std::declval
  2020-09-01  2:07 [Bug c++/96877] New: Erroneous warning when default initializing function pointer types defined using std::declval insertinterestingnamehere at gmail dot com
  2020-09-01 15:29 ` [Bug c++/96877] " insertinterestingnamehere at gmail dot com
  2020-09-01 17:37 ` redi at gcc dot gnu.org
@ 2020-09-01 19:19 ` insertinterestingnamehere at gmail dot com
  2020-09-01 19:25 ` insertinterestingnamehere at gmail dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: insertinterestingnamehere at gmail dot com @ 2020-09-01 19:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Ian Henriksen <insertinterestingnamehere at gmail dot com> ---
The goal of doing it that way was get the exception specification onto the
pointer type in C++11 and C++14. The intent was to get the equivalent of

typedef void(*function_type)(void*) noexcept;

but with standards earlier than C++17.

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

* [Bug c++/96877] Erroneous warning when default initializing function pointer types defined using std::declval
  2020-09-01  2:07 [Bug c++/96877] New: Erroneous warning when default initializing function pointer types defined using std::declval insertinterestingnamehere at gmail dot com
                   ` (2 preceding siblings ...)
  2020-09-01 19:19 ` insertinterestingnamehere at gmail dot com
@ 2020-09-01 19:25 ` insertinterestingnamehere at gmail dot com
  2020-09-02  8:51 ` redi at gcc dot gnu.org
  2020-09-02 16:51 ` insertinterestingnamehere at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: insertinterestingnamehere at gmail dot com @ 2020-09-01 19:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Ian Henriksen <insertinterestingnamehere at gmail dot com> ---
It's worth noting that, with g++

using function_type = void (*)(void*) noexcept;

actually works, but

typedef void(*function_type)(void*) noexcept;

does not. clang++ rejects both though.

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

* [Bug c++/96877] Erroneous warning when default initializing function pointer types defined using std::declval
  2020-09-01  2:07 [Bug c++/96877] New: Erroneous warning when default initializing function pointer types defined using std::declval insertinterestingnamehere at gmail dot com
                   ` (3 preceding siblings ...)
  2020-09-01 19:25 ` insertinterestingnamehere at gmail dot com
@ 2020-09-02  8:51 ` redi at gcc dot gnu.org
  2020-09-02 16:51 ` insertinterestingnamehere at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-09-02  8:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Ian Henriksen from comment #3)
> The goal of doing it that way was get the exception specification onto the
> pointer type in C++11 and C++14. The intent was to get the equivalent of
> 
> typedef void(*function_type)(void*) noexcept;
> 
> but with standards earlier than C++17.

But you can't do that. There is no difference between void(*)(void*) noexcept
and void(*)(void*) in C++14, they're the same type. It doesn't matter whether
you write the type out, or do remove_reference_t<decltype(std::declval<T>())>,
the type is the same.

Concretely, it doesn't work:

#include <utility>

using function_type = decltype(std::declval<void (*)(void*) noexcept>());
static_assert( noexcept(std::declval<function_type>()(nullptr)), "" );

The assertion fails before C++17, because the type does not have noexcept in
it.

(In reply to Ian Henriksen from comment #4)
> It's worth noting that, with g++
> 
> using function_type = void (*)(void*) noexcept;
> 
> actually works,

You can write that declaration, but the noexcept still isn't part of the type:

#include <utility>

using function_type = void (*)(void*) noexcept;
static_assert( noexcept(std::declval<function_type>()(nullptr)), "" );

If you need C++17 then use C++17.

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

* [Bug c++/96877] Erroneous warning when default initializing function pointer types defined using std::declval
  2020-09-01  2:07 [Bug c++/96877] New: Erroneous warning when default initializing function pointer types defined using std::declval insertinterestingnamehere at gmail dot com
                   ` (4 preceding siblings ...)
  2020-09-02  8:51 ` redi at gcc dot gnu.org
@ 2020-09-02 16:51 ` insertinterestingnamehere at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: insertinterestingnamehere at gmail dot com @ 2020-09-02 16:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Ian Henriksen <insertinterestingnamehere at gmail dot com> ---
Thanks, this makes sense. I originally got this idea from
https://stackoverflow.com/a/27489923. The discussion there implied there was
some kind of ambiguity in the standard and showed some examples where exception
specification appeared to be carried through as a part of the type in order to
satisfy the rules about assigning to function pointers with noexcept
specifiers. After revisiting their examples I've found that there's actually
inconsistency with how actual values are handled vs how values from
std::declval are handled. For example, the following is accepted as valid C++11
by both gcc and clang:


#include <type_traits>
#include <utility>

void (*function_ptr)(void *) noexcept = nullptr;
using function_type = decltype(function_ptr);

using function_type_2 = std::remove_reference<decltype(std::declval<void
(*)(void*) noexcept>())>::type;

function_type thing1;
function_type_2 thing2;
static_assert(noexcept(thing1(nullptr)), "");
static_assert(!noexcept(thing2(nullptr)), "");
static_assert(std::is_same<function_type, function_type_2>::value, "");


See https://godbolt.org/z/YbzGM3.

It's not obvious to me what the correct behavior would be based off of the
actual wording of the standard, but this is bizarre. This behavior is
consistent across clang and the last 3 major gcc releases, but the implicit
inclusion of the exception specification in the type does not seem like
something that should be relied upon.

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

end of thread, other threads:[~2020-09-02 16:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-01  2:07 [Bug c++/96877] New: Erroneous warning when default initializing function pointer types defined using std::declval insertinterestingnamehere at gmail dot com
2020-09-01 15:29 ` [Bug c++/96877] " insertinterestingnamehere at gmail dot com
2020-09-01 17:37 ` redi at gcc dot gnu.org
2020-09-01 19:19 ` insertinterestingnamehere at gmail dot com
2020-09-01 19:25 ` insertinterestingnamehere at gmail dot com
2020-09-02  8:51 ` redi at gcc dot gnu.org
2020-09-02 16:51 ` insertinterestingnamehere at gmail 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).