Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::byte in C++ 17 equivalent to byte in C#?

I just noticed std::byte in the C++ 17.

I am asking this question because I use the code below to send byte array to C++ to play audio sound.

C#:

[DllImport ("AudioStreamer")]
public static extern void playSound (byte[] audioBytes);

C++:

#define EXPORT_API __declspec(dllexport)
extern "C" void EXPORT_API playSound(unsigned char* audioBytes)

With the new byte type in C++ 17, it looks like I might be able to do this now:

C#:

[DllImport ("AudioStreamer")]
public static extern void playSound (byte[] audioBytes);

C++:

#define EXPORT_API __declspec(dllexport)
extern "C" void EXPORT_API playSound(byte[] audioBytes)

I am not sure if this will even work because the compiler I use does not support byte in C++ 17 yet.

So, is std::byte in C++ 17 equivalent to byte in C#? Is there a reason to not use std::byte over unsigned char* ?

like image 269
Programmer Avatar asked Aug 31 '25 18:08

Programmer


1 Answers

According to C++ reference,

Like the character types (char, unsigned char, signed char) std::byte can be used to access raw memory occupied by other objects.

This tells me that you can freely replace

unsigned char audioBytes[]

with

std::byte audioBytes[]

in a function header, and everything is going to work, provided that you plan to treat bytes as bytes, not as numeric objects.

like image 52
Sergey Kalinichenko Avatar answered Sep 02 '25 06:09

Sergey Kalinichenko