AT45Status
is a struct with several members and I have the following function:
/**
* @brief read the chip status from the flash chip
* @retval a pointer to the current status of the chip
*/
AT45Status const * const ReadChipStatus(void) {
uint8_t commands[1] = {STATUS_READ};
static AT45Status status;
//// … details, but the result is updated values inside status
return &status;
}
I get the following warning if I compile this using C++:
…(16): warning: #815-D: type qualifier on return type is meaningless
I of course looked up an exact explanation and it is a relevant warning for a trivial type which is value semantically passed. But I'm returning a pointer to an object which I don't want to be changed. (pointer or the data pointed to). As such the type qualifier is meaningful.
The warnings disappear when I change the return statement into:
return (AT45Status const * const)&status;
Why can't/doesn't the compiler implicitly cast an AT45Status*
into an AT45Status const * const
? What am I missing in this interaction?
The warning is trying to tell you that you're trying to return a const
pointer, which is meaningless, because the returned pointer itself can't be modified.
On the other hand, qualify the object being pointed as const
is meaningful (to make the object pointed to unmodifiable), i.e. returning a pointer to const
. e.g.
AT45Status const * ReadChipStatus(void)