I'm trying to call this method
#define SIZE 16
void DoSomething(char(&value)[SIZE])
{
}
From this method:
void BeforeDoingSomething(char* value, int len)
{
if (len == SIZE)
{
DoSomething(value);
}
}
Attempting to do this gives me this error:
a reference of type "char (&)[16]" (not const-qualified) cannot be initialized with a value of type "char *"
Any tips for how to get the compiler to accept value
passed in the function BeforeDoingSomething
?
As the error explains, you cannot initialize a reference to an array using a pointer.
If and only if you can prove that value
does in fact point to (first element of) an array of appropriate type, then what you can do is explicitly reinterpret the pointer, and indirect it:
DoSomething(*std::launder(reinterpret_cast<char(*)[SIZE]>(value)));
You can do it like this, but only in the case if there is a char[16]
at the address where value
points:
DoSomething(*std::launder(reinterpret_cast<char (*)[16]>(value)));
It is the same case as the first example here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With