Search code examples
c++constantsconst-correctness

Why does the compiler warn about const on a return type being meaningless?


I am trying to use a return type of const MyClass * const. However, I get a warning:

Warning: #815-D: type qualifier on return type is meaningless.

Is this not a valid type? I want a pointer than cannot be changed, and I want the thing it points to to not be changed either.


Solution

  • The pointer itself has value type, so it doesn't make sense to make it const. What the caller function does with the returned value can't be restricted by the called function. This is akin to trying to define something like:

    const int getInt();
    

    getInt(), in this case, just returns an int value (not a reference). It goes to a register, then the caller function receives it and does whatever it wants with it.