public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/69818] warn for C++ functional cast expression on pointer or reference (i.e. add -Wfunctional-cast)
       [not found] <bug-69818-4@http.gcc.gnu.org/bugzilla/>
@ 2022-01-23 14:24 ` f.heckenbach@fh-soft.de
  2023-03-18  2:27 ` egallager at gcc dot gnu.org
  1 sibling, 0 replies; 2+ messages in thread
From: f.heckenbach@fh-soft.de @ 2022-01-23 14:24 UTC (permalink / raw)
  To: gcc-bugs

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

Frank Heckenbach <f.heckenbach@fh-soft.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |f.heckenbach@fh-soft.de

--- Comment #8 from Frank Heckenbach <f.heckenbach@fh-soft.de> ---
Some such cases, including the original example, will run afoul of aliasing
rules and at least produce a warning about that.

However, that's not the case when the target is char &, since char is
explicitly allowed to alias. So here's an example that doesn't produce any kind
of warning with any option I tried:

#include <iostream>

int main ()
{
  float x = 1.2345f;
  using T = char &;
  std::cout << +T (x);
}

It outputs 25 on x86_64 (LE) rather than 1 as with a value cast to char.

I agree that it may occur unnoticed in generic programming where a type
parameter may easily become of reference type.

I don't see any reason why someone would intentionally want to use this
"feature" -- if one wants a reinterpret cast, one should write
reinterpret_cast.

So I agree a warning would be very useful -- I'd even say it should be included
in -Wall or -Wextra if not on by default.

See also:
https://quuxplusone.github.io/blog/2020/01/22/expression-list-in-functional-cast/

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

* [Bug c++/69818] warn for C++ functional cast expression on pointer or reference (i.e. add -Wfunctional-cast)
       [not found] <bug-69818-4@http.gcc.gnu.org/bugzilla/>
  2022-01-23 14:24 ` [Bug c++/69818] warn for C++ functional cast expression on pointer or reference (i.e. add -Wfunctional-cast) f.heckenbach@fh-soft.de
@ 2023-03-18  2:27 ` egallager at gcc dot gnu.org
  1 sibling, 0 replies; 2+ messages in thread
From: egallager at gcc dot gnu.org @ 2023-03-18  2:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Eric Gallager <egallager at gcc dot gnu.org> ---
I've learned that there's another name for function-style casts that people
sometimes use: constructor-style casts. So, there might be some bikeshedding to
do about the name of the option...

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

end of thread, other threads:[~2023-03-18  2:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-69818-4@http.gcc.gnu.org/bugzilla/>
2022-01-23 14:24 ` [Bug c++/69818] warn for C++ functional cast expression on pointer or reference (i.e. add -Wfunctional-cast) f.heckenbach@fh-soft.de
2023-03-18  2:27 ` egallager 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).