Let's say I have defined a zero_initialize()
function:
template<class T>
T zero_initialize()
{
T result;
std::memset(&result, 0, sizeof(result));
return result;
}
// usage: auto data = zero_initialize<Data>();
Calling zero_initialize()
for some types would lead to undefined behavior1, 2. I'm currently enforcing T
to verify std::is_pod
. With that trait being deprecated in C++20 and the coming of concepts, I'm curious how zero_initialize()
should evolve.
- What (minimal) trait / concept can guarantee memsetting an object is well defined?
- Should I use
std::uninitialized_fill
instead ofstd::memset
? And why? - Is this function made obsolete by one of C++ initialization syntaxes for a subset of types? Or will it be with the upcoming of future C++ versions?
1) Erase all members of a class.
2) What would be reason for “undefined behaviors” upon using memset on library class(std::string)? [closed]