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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #3)
> Maybe we could suppress the warning for explicit type conversions using
> functional notation, i.e. A(a), when the copy constructor is user-provided.
> 
> It's not a perfect heuristic. It would still warn for (A)a and
> static_cast<A>(a) which have identical semantics according to the standard.
> And suppressing the warning for user-provided copy constructors would mean
> we still warn for a type like struct B : A { }; even though it should
> probably be treated just like A. Suppressing the warning for non-trivial
> copy constructors would miss warnings for anything including a std::string
> or other non-trivial type, even though the copy might really be useless.
> 
> But at least there would be a way to alter code to avoid the warning, which
> could be documented.

Why would user-provided copy ctor matter?  In this testcase there is no
user-provided copy ctor, what matters is that the static cast is handled
through
perform_direct_initialization_if_possible as direct initialization of a
temporary, without the cast there is no temporary.

-Wuseless-cast complains about all these below, and none of the casts are
useless, all of them create a temporary.  So perhaps not warn at all if
CLASS_TYPE_P (type)?

struct S { int s; void bump () { s++; } };

void
foo ()
{
  S s = { 1 };
  s.bump ();
  S (s).bump ();
  ((S) s).bump ();
  static_cast<S> (s).bump ();
}

Reply via email to