Is there a type trait (or concept) to identify those types for which the following is safe?
template <typename T>
std::enable_if_t<std::some_type_trait<T>::value> Write(std::ostream &os,const T &x)
{ os.write(reinterpret_cast<const char *>(&x),sizeof(T)); }
template <typename T>
std::enable_if_t<std::some_type_trait<T>::value> Read(std::istream &is,T &x)
{ is.read(reinterpret_cast<char *>(&x),sizeof(T)); }
I’m thinking of classes containing POD excluding pointers (but not arrays). Something like StandardLayoutType
s but without pointers. I neither want to constrain the objects to be TrivialType
nor TriviallyCopyable
.
Sorry if I’m inaccurate. I know very little of data representation.
Given a 1st parameter of s
, the read
method:
Extracts characters and stores them into successive locations of the character array whose first element is pointed to by
s
So your real question is: If I have initialized an object by writing a string of bytes to it's address, is it valid?
This is the concept of Value Representation. And the value representation of Trivially Copyable types is such that:
Copying the bytes occupied by the object in the storage is sufficient to produce another object with the same value
Thus you want to ensure that your object is Trivially Copyable this isn't per say a standard concept but it can be succinctly defined as:
- Every copy constructor is Trivial or deleted
- Every move constructor is Trivial or deleted
- Every copy assignment operator is Trivial or deleted
- Every move assignment operator is Trivial or deleted
- At least one copy constructor, move constructor, copy assignment operator, or move assignment operator is non-deleted
- Trivial non-deleted destructor
The spirit of the assertion that at least one Trivial initializer exists for the object boils down to these requirements of a Trivially Copyable type, it's non-static members, and any of it's base classes:
As far as the requirement of a Trivial destructor:
- The destructor is not user-provided (meaning, it is either implicitly declared, or explicitly defined as defaulted on its first declaration)
- The destructor is not virtual (that is, the base class destructor is not virtual)
- All direct base classes have trivial destructors
- All non-static data members of class type (or array of class type) have trivial destructors
Having fully defined what it means to be a Trivially Copyable type, it is impossible for a "type trait or concept" to determine whether all of these requirements are met in all cases, for example: A type that defines a Trivial Initializer with a signature that matches the default initializer may or may not be Trivially Copyable contingent on the code which initializes the type in that initializers body; For such a type, the only way to determine if it's Trivially Copyable is human inspection of the initializer. If however you are willing to tighten the requirements to what is detectable, is_trivially_copyable
will guarantee that your type is Trivially Copyable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With