C++ Logo


Advanced search

BytesReadable & BytesWritable Traits

From: connor horman <chorman64_at_[hidden]>
Date: Sun, 1 Dec 2019 13:24:46 -0500
I am working on a library in C++ that has IO Capabilities, and as part of
the library, I define a couple of named requirements that types being
written or read “raw” (IE. w/o using a special encoding function) have to
satisfy (otherwise the read/write has undefined behaviour, usually because
it either causes UB straight up, or either leads to UB or an inconsistent
state). I was wondering if they could be attached to fread/fwrite for the
same reason.
The named requirements are defined as follows
BytesReadable (for fread):
A type T satisfies bytes readable if T satisfies TriviallyCopyable, and an
object of type T, and all subobjects of that object:
* Are neither a const nor volatile objects (note that modifying a
const-object through a non-const lvalue is UB, and accessing a
volatile-object through a non-volatile lvalue is UB)
* Is not a pointer or pointer-to-member (such a pointer being read can’t be
used whatsoever due to limitations on pointers, and the value would
unconditionally be an invalid pointer)
* Has no non-static data members of a reference type (for obvious reasons)

BytesWritable is the same, except the limitation on const objects is lifted.

A standard layout structure type would satisfy both concepts if it had no
const or volatile non-static data members, no non-static data members of a
reference type, and the types of all members satisfy the concept
And array of a type that satifies either will satisfy that concept.
If T satisfies BytesWritable, const T also satisfies BytesWritable.
A union type satisfies either if the types of all non-static data members
satisfies that concept.
All empty types, which are not cv-qualified, satisfy both concepts
Aside from a few types, and types guaranteed by the standard to be empty,
no types in the standard library would satisfy this concept.

Received on 2019-12-01 12:27:20