My goal is to define a clean API for my library.
One of my function returns a pointer that shall not be modified with pointers arithmetic.
To do so at compile-time, I was planning on using the const
keyword in the function prototype.
Here's a naive example:
int global_var = 12;
int* const access_global_var(){ return &global_var;}
int main(void) {
int* const ptr = access_global_var();
*ptr = 15; //< Should be valid
ptr++; //< Should be Invalid
return 0;
}
It works as expected: the compiler throws an error at ptr++;
.
Problem:
When compiling with the -Wextra
clang/gcc flag, I get the following warnings:
warning: 'const' type qualifier on return type has no effect
Is this warning correct? Is there something I'm missing? Is there a better way to achieve what I'm trying to do?
As warnings are treated errors in my project, this is, as you can expect, problematic.
Thanks