I have variable input
type const std::string&
:
const std::string& input
Now I need to convert this to const unsigned char*
because this is the input of the function.
Unitl now I have correct code for converting:
reinterpret_cast<const unsigned char*>(input.c_str())
This works well, but in clang I got a warning:
do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]
What is the correct way to change a string
or const char*
to const unsigned char*
?
What is the correct way to change a string or const char* to const unsigned char*?
The correct way is to use reinterpret_cast.
If you want to avoid reinterpret_cast, then you must avoid the pointer conversion entirely, which is only possible by solving the XY-problem. Some options:
std::basic_string<unsigned char>
in the first place.std::ranges::views::transform
which uses static cast for each element.unsigned char*
to accept char*
instead.If you cannot change the type of input
and do need a unsigned char*
and you still must avoid reinterpret cast, then you could create the std::basic_string<unsigned char>
from the input using the transform view. But this has potential overhead, so consider whether avoiding reinterpret_cast is worth it.