Is std :: byte in C ++ 17 equivalent to a byte in C #?
I just noticed std::byte
in C ++ 17.
I am asking this question because I am using the following code to send a byte array in C ++ to play audio.
FROM#:
[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 type byte
in C ++ 17, it looks like I can do this now:
FROM#:
[DllImport ("AudioStreamer")]
public static extern void playSound (byte[] audioBytes);
C ++:
#define EXPORT_API __declspec(dllexport)
extern "C" void EXPORT_API playSound(byte[] audioBytes)
I'm not sure if this will work because the compiler used does not support it byte
in C ++ 17.
So, std::byte
in C ++ 17, is it equivalent byte
in C #? Is there a reason not to use std::byte
over unsigned char*
?
source to share
Like character types (char, unsigned char, signed char)
std::byte
can be used to access the raw memory occupied by other objects.
It tells me that you are free to replace
unsigned char audioBytes[]
from
std::byte audioBytes[]
in the function header and everything will work if you plan on treating bytes as bytes and not as numeric objects.
source to share
std::byte
It is equivalent to both unsigned char
, and char
in C ++ in a way, that a type of 1 byte represents untreated memory.
If you used unsigned char*
in your interface, you can easily replace it with std::byte
.
In C # code this will not make any changes, on the C ++ side it will make your type system stricter (which is good) due to the fact that you will not be able to treat yours std::byte
as text characters or as small integers.
Of course this is a C ++ 17 feature that may or may not be properly supported by your compiler.
source to share