This might actually be slightly less off topic than the previous post,
because it parallels the way that I think about that condition::wait
method... Anyway, I'm curious what people think about:
void perverse_strcpy(
const char *src,
char *dest,
const char *done // note const
) {
while (!done) { // note we expect change
*dest++ = *src++;
}
}
The caller can do something like:
src = "hello world";
dest[strlen(src)] = 1; // store a nonzero value
perverse_strcpy(src, dest, dest+strlen(src));
Yes, its a kinda stupid and, well, dubious function. However, the point is
that "const char *" simply means that the pointer points to a char that can
only be accessed as a "const char" through that pointer. It does not imply
that the referenced thing is physically const. Nor does it imply that the
referenced thing is logically const. (meaning that non-const methods can
still be called on an object that has a "const Foo *" pointing to it; they
simply can't be called through that pointer.)
Does this run counter to what people believe the semantics of "const char *"
to be? Are compilers permitted to optimize out the check of "!done" based
on const-ness? (IIRC, they actually may in some related examples, if "src"
and "dest" are types that should not alias to "done"; but this is not the
case here) Is there any undefined or implementation-defined behavior of the
above code?
As separate aside, I believe that "physically const" refers specifically to
a property of an object, say, the string constant "x". It is the reason
why:
const_cast