If I do:
const char* const_str = "Some string";
char* str = const_cast<char*>(const_str); // (1)
str[0] = "P"; // (2)
Where (which line) exactly is the undefined behavior ?
I've been searching a lot for this on SO but haven't found any explicit and precise answer (or at least, none that I could understand).
Also related: if I use an external library which provides this kind of function:
// The documentation states that str will never be modified, just read.
void read_string(char* str);
Is it ok to write something like:
std::string str = "My string";
read_string(const_cast<char*>(str.c_str()));
Since I know for sure that read_string()
will never try to write to str
?
Thank you.