Date: Mon, 3 Aug 2020 15:49:56 +0200
If I understand correct then in C++14 to read memory of object like e.g.
float (it is required that the type is trivially-copyable) you should:
copy it with std::memcpy, then read it
through unsigned char * (or reference)?
Reading through just "char*" (or reference) is also permitted. Unsigned
char is not.
This rule results from standard about glvalue[1], copying memory to
array[3] and then reading the array[2].
Is this correct?
In c++17 stype std::byte joins the two magical types that can be used to
read memory.
For some types, the memcpy() operation is not required, right?
If the object to read was actually allocated as new char[] then you can
read it by dereferencing char*.
You can read it also by dereferencing unsigned char* and std::byte*
right? As in following example:
auto buf = new unsigned char[10];
std::byte * p1 = reinterpret_cast<std::byte*>(buf);
*(p1+3) = std::byte(42);
char * p2 = reinterpret_cast<char*>(buf);
*(p2+3) = 42;
So the types of the pointers used to access (like p1 here)
can be unsigned char, char, std::byte(since c++17).
What are the allowed types for the buffer allocation new ....[10] for
this to work?
I assume unsigned char, char, signed char, std::byte.
What about enum class that uses one of this 3 as underlying type?
What about other fundamental 1-byte types, e.g. integral types?
Is this valid for all 1-byte tirivialy-copyable objects?
How it is for C++14, and what changed in C++17 and C++20 about this?
[1] http://eel.is/c++draft/basic.lval#11.3 - we must read as "a char,
unsigned char, or std::byte type" (but not as signed char!); C++14
https://timsong-cpp.github.io/cppwp/n4140/basic.lval#10.8
[2] http://eel.is/c++draft/expr.add#4 - to access i-th element of array;
see http://eel.is/c++draft/dcl.array for what is an array
[3] http://eel.is/c++draft/basic.types#2 - we can read any
trivially-copyable type T after we std::memcpy it
float (it is required that the type is trivially-copyable) you should:
copy it with std::memcpy, then read it
through unsigned char * (or reference)?
Reading through just "char*" (or reference) is also permitted. Unsigned
char is not.
This rule results from standard about glvalue[1], copying memory to
array[3] and then reading the array[2].
Is this correct?
In c++17 stype std::byte joins the two magical types that can be used to
read memory.
For some types, the memcpy() operation is not required, right?
If the object to read was actually allocated as new char[] then you can
read it by dereferencing char*.
You can read it also by dereferencing unsigned char* and std::byte*
right? As in following example:
auto buf = new unsigned char[10];
std::byte * p1 = reinterpret_cast<std::byte*>(buf);
*(p1+3) = std::byte(42);
char * p2 = reinterpret_cast<char*>(buf);
*(p2+3) = 42;
So the types of the pointers used to access (like p1 here)
can be unsigned char, char, std::byte(since c++17).
What are the allowed types for the buffer allocation new ....[10] for
this to work?
I assume unsigned char, char, signed char, std::byte.
What about enum class that uses one of this 3 as underlying type?
What about other fundamental 1-byte types, e.g. integral types?
Is this valid for all 1-byte tirivialy-copyable objects?
How it is for C++14, and what changed in C++17 and C++20 about this?
[1] http://eel.is/c++draft/basic.lval#11.3 - we must read as "a char,
unsigned char, or std::byte type" (but not as signed char!); C++14
https://timsong-cpp.github.io/cppwp/n4140/basic.lval#10.8
[2] http://eel.is/c++draft/expr.add#4 - to access i-th element of array;
see http://eel.is/c++draft/dcl.array for what is an array
[3] http://eel.is/c++draft/basic.types#2 - we can read any
trivially-copyable type T after we std::memcpy it
Received on 2020-08-03 08:53:26