With the standard std :: byte standard, when do we use void * and when is byte *?

C ++ 17 will include a std::bytetype for a single atomic address memory unit having 8 bits on typical computers.

There is already a small dilemma before this standardization when referring to raw memory - between using char*/ unsigned char*on the one hand or void *on the other. Now one of the reasons for the preference has void *been deleted - it std::bytedoes not have the same connotations as char; it's about raw memory, not about symbols.

So my question is: what is a good rule of thumb, in days std::byte, regarding when you prefer it over void *and when it's the other way around?


Of course, when you are dealing with old code or C code, you are limited in what it accepts; Basically, I mean the new code in which you can select all types.

+4
source share
3 answers

What is the motivation for std::byte?

Quote from original paper ;

- . char, signed char unsigned char. " ". , . - , , , - , .

, std::byte , "" char, , , , , , -refernce, .

std::byte , char; ,

, std::byte char - ( ). .

, std::byte, , void * ?

, , . , , char * .. , void *, , , byte * char *.

, , void *, , . void * " -", - , , .

, uintptr_t ( intptr_t), , , .

... , .

, byte *. void * ( ).

+2

( , , -).

: ?

  • char * , - .
  • void * , .. , - , .
  • byte * , , - .

:

  • void */char *, ++ , byte * - byte * , , ++.

void * my_custom_malloc(size_t size) -
byte * my_custom_malloc(size_t size) -

struct buffer_t { byte* data; size_t length; my_type_t data_type; } -
struct buffer_t { void* data; size_t length; my_type_t data_type; } -

+2

-, void * , C , , extern "C".

std::byte - . , :

std::byte *arr = ...;
arr[i] = std::byte{0x2a};

, , , .

, void * , , ( char byte), .

, std::byte , , void * , ( a void *) .

But the real use case for void *should become more and more unusual in modern C ++, at least at a high level, because these opaque zones should usually be hidden in higher-level classes with methods for processing them. Therefore, IMHO void *should ultimately be limited to C (and older versions in C ++), compatibility, and low-level code (such as code highlighting).

+1
source

Source: https://habr.com/ru/post/1682745/


All Articles