[Bug c++/98835] False positive -Wclass-memaccess with class with ref-qualified copy-assignment operator

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Jan 26 19:38:53 GMT 2021


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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
           Keywords|                            |diagnostic
                 CC|                            |msebor at gcc dot gnu.org
         Resolution|---                         |INVALID

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The warning is issued on the basis that the left-hand operand of Bad's
assignment cannot be a temporary.  I.e., that this isn't valid: Bad() = Bad(). 
In the modified test case below the warning detects the memcpy call bypassing
the constraint the assignment operator puts on assigning to objects of the
class.  So unless there's a compelling use case showing otherwise I'm inclined
to say the warning is justified and helpful here (its wording shouldn't be
expected to perfectly reflect all the subtleties involved in the decision). 
There isn't a test case for this in the test suite so let me add this one.

  class Bad {
   public:
      Bad* operator& () { return this; }
      Bad & operator=(Bad const &) & = default;
  };

  template<typename T>
  void test() {
      static_assert (__has_trivial_copy (T));
      // T () = T ();                     // error
      memcpy (&T (), &T (), sizeof(T));   // warning
  }

  int main() {
      test<Bad>();
  }


More information about the Gcc-bugs mailing list