c++ - Type trait to identify types that can be read/written in binary form -


is there type trait (or concept) identify types following 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). standardlayouttypes without pointers. neither want constrain objects trivialtype nor triviallycopyable.

sorry if i’m inaccurate. know little of data representation.

given 1st parameter of s, read method:

extracts characters , stores them successive locations of character array first element pointed s

so real question is: if have initialized object writing string of bytes it's address, valid?

this concept of value representation. , value representation of trivially copyable types such that:

copying bytes occupied object in storage sufficient produce object same value

thus want ensure object trivially copyable isn't per standard concept can succinctly defined as:

the spirit of assertion @ least 1 trivial initializer exists object boils down these requirements of trivially copyable type, it's non-static members, , of it's base classes:

  1. it's given trivial initializer is, or behaves as, corresponding default intializer
  2. it has no virtual methods
  3. it has no members of volatile-qualified type

as far requirement of trivial destructor:

  • the destructor not user-provided (meaning, either implicitly declared, or explicitly defined defaulted on first declaration)
  • the destructor not virtual (that is, base class destructor 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 defined means trivially copyable type, impossible "type trait or concept" determine whether of these requirements met in cases, example: type defines trivial initializer signature matches default initializer may or may not trivially copyable contingent on code initializes type in initializers body; such type, way determine if it's trivially copyable human inspection of initializer. if willing tighten requirements is detectable, is_trivially_copyable guarantee type trivially copyable.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -