Should I return bool or const bool?

Ben Hymers picture Ben Hymers · Sep 18, 2009 · Viewed 25k times · Source

Which is better:

bool MyClass::someQuery() const;

const bool MyClass::someQuery() const;

I've been using 'const bool' since I'm sure I remember hearing it's "what the ints do" (for e.g. comparison operators) but I can't find evidence of that anywhere, mostly due to it being difficult to Google and Intellisense not helping out any ;) Can anyone confirm that?

To me returning const values (this isn't just about bools) makes more sense; it'll prevent temporaries being modified, which is almost always going to be a programmer mistake. I just want something to back that up so I can extol returning const values to my colleagues :)

Answer

sharptooth picture sharptooth · Sep 18, 2009

This is the case when const adds no value but inflates the code and makes the reader think more. What's the point of this const? The caller can copy the value into some non-const variable and do whatever he wants with it anyway.