On Fri, Jul 06, 2018 at 11:14:41AM +1200, Soul Studios wrote:
> Simply because a struct has a constructor does not mean it isn't a
> viable target/source for use with memcpy/memmove/memset.
> Having benchmarked the alternatives memcpy/memmove/memset definitely
> makes a difference in various scenarios.
> The bypass of littering code with needless reinterpret_cast<void *>'s is
> fugly.
> Members which are invariants should of course be noted, but anyone using
> memset/cpy/move probably knows this.
> Please discuss this annoyance. I would prefer this be moved into Extra,
> as this is less commonly used.
'-Wclass-memaccess (C++ and Objective-C++ only)'
Warn when the destination of a call to a raw memory function such
as 'memset' or 'memcpy' is an object of class type, and when
writing into such an object might bypass the class non-trivial or
deleted constructor or copy assignment, violate const-correctness
or encapsulation, or corrupt virtual table pointers. Modifying the
representation of such objects may violate invariants maintained by
member functions of the class. For example, the call to 'memset'
below is undefined because it modifies a non-trivial class object
and is, therefore, diagnosed. The safe way to either initialize or
clear the storage of objects of such types is by using the
appropriate constructor or assignment operator, if one is
available.
std::string str = "abc";
memset (&str, 0, sizeof str);
The '-Wclass-memaccess' option is enabled by '-Wall'. Explicitly
casting the pointer to the class object to 'void *' or to a type
that can be safely accessed by the raw memory function suppresses
the warning.
If you have examples where you currently get this warning but the code is
in fact correct, please let us know (in bugzilla, preferably), so that
the warning can be improved.
Segher