Why const int instead of just int

1
2
3
4
const int func(void)
{
    return 3;
}


In code above, what is the need of const int as returned type instead of just int? Can you site a specific situation when it is needed?
the const here is part of the type. it returns a constant int. That is useful when the return type is a pointer or a reference; it is not normally useful for other types though it may help avoid a warning here and there. Most uses of const are programmer aids to prevent bugs where you accidentally modify a value you should not have. Almost all uses of const could be taken out and it would work fine, but putting them in throws an error that protects you from messing up. This is akin to private class members: it works fine if everything is public (assuming no screw ups), it just opens the door to mistakes when something is modified in an unexpected way or at an unexpected time (the screw ups I mentioned).

As jonnin said, using const can be useful when returning references or pointers (to specify that the object being referred to should not be modified) but there is no reason to use it for a simple int return type like you're doing here.

GCC with -Wignored-qualifiers (one of the flags enabled by -Wextra) generates a warning when compiling your code:

warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
 const int func(void)
 ^~~~~


jonnin wrote:
Almost all uses of const could be taken out and it would work fine, but putting them in throws an error that protects you from messing up.

It's true for some uses of const, but it's definitely not true for all uses.

If you for example have a function that accepts a reference to const...
 
void foo(const T&);
and then you remove the const
 
void foo(T&);
then you would no longer be able to call that function with a const objects.
1
2
const T obj;
foo(obj); // worked before but now gives an error. 
So now you need to go and change the definition of the object so that it also doesn't use const
1
2
T obj;
foo(obj); // OK (although it's no longer clear from just looking a the code whether the object will be modified or not) 

Sometimes the object that you pass to the function is not declared right next to the function call like in the example above. Instead it might be passed as an argument to the function which might come as an argument from yet another function and so on. So removing a single const actually forces you to have to go and remove const in a lot of other places too.

So when dealing with pointers and references (and member functions) you need to be consistent in your usage of const. But in other places (e.g. when declaring a local variable) you can often leave out const without breaking code elsewhere.
@Peter wrote:
one of the flags enabled by -Wextra

Thank you @Peter, You taught me to turn on/off two compiler flags today)))

thank you @jonnin too,

I will ask about const used with pointers and references later.
Peter, I agree, but

So now you need to go and change the definition of the object so that it also doesn't use const

well, the idea was cutting the keyword out everywhere :P
there are a few things that will break if you do that. But if its removed *everywhere* most stuff would probably work. Trying to think of something were if its stripped off both ends, it breaks, but while I know its out there, I can't think of an example right now.
A series about using const, starting with const local variables.
https://www.sandordargo.com/blog/2020/11/04/when-use-const-1-functions-local-variables

What seems to be largely ignored as well is using constexpr vs. const.
https://stackoverflow.com/questions/14116003/whats-the-difference-between-constexpr-and-const
Topic archived. No new replies allowed.