[Bug c++/98660] -Wold-style-cast should not warn on casts that look like (decltype(x))(x)

gasper.azman at gmail dot com gcc-bugzilla@gcc.gnu.org
Sat Jan 16 21:54:04 GMT 2021


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.


More information about the Gcc-bugs mailing list