Suppose I have a weired add function which does an addition and an increment then stores the result to somewhere else.
void add(const int* a, const int* b, int* c) {
*c = (*a)++ + *b;
}
If I pass the same pointer to a and c,
int a = 1, b = 3;
add(&a, &b, &a);
Then it will eventually modify the value pointed by a.
My questions:
- Does this violate the const qualifier for the first argument?
- Is the operation undefined as the typical unsequenced
x = x++is?
I feel the answer to both questions should be YES, but my gcc gives a warning for neither.
EDIT: I know there can be a better workaround for the sake of the code. My question is more on the language feature itself.
You have
This doesn't compile.
We could remove the
const.But then we encounter undefined behaviour when
a == c || a == b. We can signal this using therestrictkeyword.With this signature, it becomes UB to call
addwith two identical arguments.