I'm one of these types of people who thinks "if it shouldn't be able to change, then it should be const
". Perhaps I take const
to the extreme by doing this, but, since my function return types generally should not be able to change, I declare all my functions to return const
. (Unless they return void
of course - does it even make sense to return by const void
?)
You probably think either I'm kind of nutty, and I'm wearing out my keyboard typing stuff I don't need to, or that returning const makes sense because you already use it yourself. Or maybe you're thinking neither of those things in which case I guessed incorrectly.
I've compiled my program using g++4.8, and enabled the compiler switch -Wextra
. When doing so, g++ warns me that it ignores every single one of my return statements. None of them are returning by const
. This makes no difference after compiling obviously, but I wanted to know is there a way of forcing g++ to compile and pay some attention to my const
return types.
More importantly, why does g++ ignore the const
-- or is it simply because I'm nutty and g++ thinks returning const
is unnecessary?
As requested: Example...
inline const bool collisionTest(...) { ... }
warning: type qualifiers ignored on function return type
It doesn't ignore it, it's just useless if you return primitive types by value.
Returning by value means you can't modify whatever it is you returned anyways, because it's an r-value. The const would be redundant.
See:
int foo();
How would you modify the return?
foo() = 4;
would yield a compiler error.
If you return a reference though, the const does matter:
int& foo();
const int& goo();
foo() = 42; //okay
goo() = 42; //error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With