Question

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 :)

Was it helpful?

Solution

So you know it's right, you're just after the Voice of Authority? Preventing accidental modification of temporaries is very valuable. In general, you should declare as many things as you possibly can const, it protects you from a variety of accidents and gives the optimiser useful hints.

D'you have a copy of Scott Meyers' "Effective C++" around? Point them at Item 3 (page 18 in the third edition) ;)

It gives the example of

class Rational {...};
const Rational operator* (const Rational& lhs, const Rational& rhs );

if( (a * b) = c ) // declaring operator *'s return value const causes error to be caught by compiler

OTHER TIPS

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.

Note that if((a*b) = c) won't compile for built-in types anyway, so it is very relevant here whether we're talking built-in types (your question asks for bool) or user-defined types.

For built-in types it makes no sense at all, so it shouldn't be used. And for user-defined types, I'm in jalf's camp: What if the caller wants to modify the returned object?

I'm not convinced that if((a*b) = c) is such a good argument for returning const user-defined types, since I can't remember the last time I've seen a compiler not warn about this.

To be a little more specific, only "objects" can be const. The C++ standard's definition of "object" includes everything an lvalue refers to ("has a name") and class-type temporaries. A boolean return value is an rvalue of a non-class type which is why a standards-compliant compiler will just ignore "const" in this case. As others said already, it's useless in this context.

When you returning a refernce to a member variable it makes sense to make it const. Here you are returning a copy, hence there is no need of const.

The const modifier is only used for return types that are returned by reference (either as reference const SomeObject& or via a pointer const SomeObject*), so the caller won't be able to modify the object via the reference/pointer. Primitive types are returned by value, which means that the caller receives a copy of the the object, not the object itself.

Therefore, const is not really appropriate for returned value types. Since the copy is outside of the control of the called function, the called function should not dictate to the caller that it cannot be changed.

It completely doesn't matter. Therefore, the consensus is to return just bool.

The reason that it doesn't matter is that you can't call non-const member functions anyway; bool is not a class or struct.

As bool is going to be copied, it's the same, to put const or not. Plus you'll may have some compil problems.

const return type

SUMMARY:

The value of a return type that is declared const cannot be changed. This is especially usefull when giving a reference to a class’s internals, but can also prevent rarer errors.

const bool func();
bool f = func();

0 errors, 0 warnings. What have you accomplished other than unnecessary code inflation?

This is an ancient post, but I think it's worth mentioning there is a potential corner case here since C++11. While, as stated by others, it will make no difference whether you use const bool or bool as return type in most cases, if you are using C++11 decltype and associates, e.g. result_of, you could declare a variable with the same type as the returning value of some function, and so the const would actually have an effect in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top