public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98660] New: -Wold-style-cast should not warn on casts that look like (decltype(x))(x)
@ 2021-01-13 17:56 gasper.azman at gmail dot com
  2021-01-14  0:42 ` [Bug c++/98660] " vanyacpp at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: gasper.azman at gmail dot com @ 2021-01-13 17:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98660
           Summary: -Wold-style-cast should not warn on casts that look
                    like (decltype(x))(x)
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gasper.azman at gmail dot com
  Target Milestone: ---

Dear GCC wizards,

Recently, the use of std::forward has been idiomatically replaced by the
following:

```
template <typename T>
void george(T&& x) {
   john((T&&)x); // means std::forward<T>(x)
}
```

Casting an `x` to `decltype(x)` is far shorter and faster to compile (which
matters since it's done in unevaluated contexts A LOT). For an example, observe
the usage in libunifex, the in-progress research implementation of the
executors proposal:

https://github.com/facebookexperimental/libunifex/blob/master/include/unifex/tag_invoke.hpp

Unfortunately, the only real way to combine this fairly important exception to
the general rules of "no c-style casts" is to disable -Wold-style-cast.

It would be a great benefit if I could leave that warning enabled, and sleep
soundly in the knowledge I didn't mistype the forwarding expression if gcc
checked for me that the type I'm casting to is, in fact, `decltype(x)`, and
complain otherwise.

Please consider this refinement for a future release of GCC.

Thank you.

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

* [Bug c++/98660] -Wold-style-cast should not warn on casts that look like (decltype(x))(x)
  2021-01-13 17:56 [Bug c++/98660] New: -Wold-style-cast should not warn on casts that look like (decltype(x))(x) gasper.azman at gmail dot com
@ 2021-01-14  0:42 ` vanyacpp at gmail dot com
  2021-01-14 11:39 ` gasper.azman at gmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: vanyacpp at gmail dot com @ 2021-01-14  0:42 UTC (permalink / raw)
  To: gcc-bugs

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

Ivan Sorokin <vanyacpp at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |vanyacpp at gmail dot com

--- Comment #1 from Ivan Sorokin <vanyacpp at gmail dot com> ---
I'm not a GCC developer, but I'm just curious.

Why the use of C-style cast is required here? Could you use static_cast
instead? I mean instead of `(decltype(x))(x)` using
`static_cast<decltype(x)>(x)`? Perhaps wrapping it in some macro in order to
not duplicate `x` twice.

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

* [Bug c++/98660] -Wold-style-cast should not warn on casts that look like (decltype(x))(x)
  2021-01-13 17:56 [Bug c++/98660] New: -Wold-style-cast should not warn on casts that look like (decltype(x))(x) gasper.azman at gmail dot com
  2021-01-14  0:42 ` [Bug c++/98660] " vanyacpp at gmail dot com
@ 2021-01-14 11:39 ` gasper.azman at gmail dot com
  2021-01-16 21:09 ` egallager at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: gasper.azman at gmail dot com @ 2021-01-14 11:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Gašper Ažman <gasper.azman at gmail dot com> ---
Ivan: indeed, you could use a static cast, or a macro - you're literally just
changing the value category of the expression to its original one. The cast is
safe.

The reason Niebler and friends (including me) are using the c-style cast is
purely because it's short, concise, unambiguous, and fast to compile.
`static_cast<decltype(x)>(x)` is a lot longer than `(T&&)x`. When you have 6 or
10 of these expressions (often with `...`s) in a function declaration (please
see the `tag_invoke` definition linked), the noise starts to matter.

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

* [Bug c++/98660] -Wold-style-cast should not warn on casts that look like (decltype(x))(x)
  2021-01-13 17:56 [Bug c++/98660] New: -Wold-style-cast should not warn on casts that look like (decltype(x))(x) gasper.azman at gmail dot com
  2021-01-14  0:42 ` [Bug c++/98660] " vanyacpp at gmail dot com
  2021-01-14 11:39 ` gasper.azman at gmail dot com
@ 2021-01-16 21:09 ` egallager at gcc dot gnu.org
  2021-01-16 21:54 ` gasper.azman at gmail dot com
  2021-01-16 21:55 ` gasper.azman at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: egallager at gcc dot gnu.org @ 2021-01-16 21:09 UTC (permalink / raw)
  To: gcc-bugs

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

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org

--- Comment #3 from Eric Gallager <egallager at gcc dot gnu.org> ---
Just don't use -Wold-style-cast then. Or disable it selectively with #pragma
GCC diagnostic

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

* [Bug c++/98660] -Wold-style-cast should not warn on casts that look like (decltype(x))(x)
  2021-01-13 17:56 [Bug c++/98660] New: -Wold-style-cast should not warn on casts that look like (decltype(x))(x) gasper.azman at gmail dot com
                   ` (2 preceding siblings ...)
  2021-01-16 21:09 ` egallager at gcc dot gnu.org
@ 2021-01-16 21:54 ` gasper.azman at gmail dot com
  2021-01-16 21:55 ` gasper.azman at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: gasper.azman at gmail dot com @ 2021-01-16 21:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Gašper Ažman <gasper.azman at gmail dot com> ---
@Eric Gallager: yes, the #pragma solution is what I currently use. It is
entirely unsatisfactory, for the reasons described in my original request.

The long-term usefulness of warnings is directly proportional to the number of
false positives they generate. If I wanted to enforce the warning in most of
the code, and only disable it where appropriate, this is what that looks like,
on a non-terrible example (borrowed from libunifex):

      #pragma GCC diagnostic push
      #pragma GCC diagnostic ignore "-Wold-style-cast"
      template <typename CPO, typename... Args>
      constexpr auto operator()(CPO cpo, Args&&... args) const
          noexcept(noexcept(tag_invoke((CPO &&) cpo, (Args &&) args...)))
          -> decltype(tag_invoke((CPO &&) cpo, (Args &&) args...)) {
        return tag_invoke((CPO &&) cpo, (Args &&) args...);
      }
      #endif

Now please, kindly tell me how I can know I didn't screw up a C-style cast in
that mess? It turns out that in the example above, ALL the casts mean
std::forward, but imagine it was a function where only some of the arguments
are forwarded, and a co-worker mistakenly puts in a c-style cast that doesn't
just change the value-category.

I'm asking for this warning to get more useful by still diagnosing c-style
casts that change more than the value category, and not diagnosing c-style
casts that are the "safest" kind - value category only, since that's what the
idiom seems to be.

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

* [Bug c++/98660] -Wold-style-cast should not warn on casts that look like (decltype(x))(x)
  2021-01-13 17:56 [Bug c++/98660] New: -Wold-style-cast should not warn on casts that look like (decltype(x))(x) gasper.azman at gmail dot com
                   ` (3 preceding siblings ...)
  2021-01-16 21:54 ` gasper.azman at gmail dot com
@ 2021-01-16 21:55 ` gasper.azman at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: gasper.azman at gmail dot com @ 2021-01-16 21:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Gašper Ažman <gasper.azman at gmail dot com> ---
s/endif/pragma GCC diagnostic pop

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

end of thread, other threads:[~2021-01-16 21:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-13 17:56 [Bug c++/98660] New: -Wold-style-cast should not warn on casts that look like (decltype(x))(x) gasper.azman at gmail dot com
2021-01-14  0:42 ` [Bug c++/98660] " vanyacpp at gmail dot com
2021-01-14 11:39 ` gasper.azman at gmail dot com
2021-01-16 21:09 ` egallager at gcc dot gnu.org
2021-01-16 21:54 ` gasper.azman at gmail dot com
2021-01-16 21:55 ` gasper.azman 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).