The title is self explanatory, but I’ll give an example anyways.
int sum(int a, int b){
return a + b;
}
We could rewrite it as
int sum(const int a, const int b){
return a + b;
}
But what about returning a const int? Is it useful in any way?
const int sum(const int a, const int b){
return a + b;
}
The caller will have to copy the const return to a const lvalue. Is this too restrictive? Do you know any use cases where this could be a good design choice?
Ok, I wasn’t clear at the question body. Think about a sum function that works similar to assembly add:
int *sum(int *a, int b){
*a += b;
return a;
}
Add the const’s to the params:
int *sum(int * const a, const int b){
*a += b;
return a;
}
What about making the return const too? Is there a reason for this?
There are a lot of situations where you may return a const pointer or a const object. I can’t imagine any real situation where you want to return a primitive type as
const.I’m not sure what’s your question, in the title your’ talking about
int* constbut in the code example you have aconst int.Some examples (in C++) follows for different combinations of
constandT*.Pointer to const (primitive type)
You have a pointer to a constant, it means you can change the pointer but you can’t change the pointed object/value. Please note that
someFunction()must not return a pointer (constor not) to a stack allocated variable. I assume the pointer is valid. For example:Pointer to const (object)
You have a pointer to a constant. It means you can change the pointer to point to another object but you can’t change the pointed object state. This doesn’t mean you can’t use that object, only that the object is const then you can use only its methods marked as const, read its fields and write its mutable fields. A constant method is defined as:
It can have any return type and any parameter the point is that it’s marked with the
constmodifier. It means that it won’t change the object state (again with the exclusion ofmutableobjects).Now an example, with
Tdeclared as:Imagine to write following code:
Constant pointer (primitive type)
You have a constant pointer, it means you can change the pointed value but you cannot assign another memory location to the pointer itself. For example:
Constant pointer (object)
You have a constant pointer, it means you can change the pointed object state but you cannot assign another memory location to the pointer itself. For example:
Constant pointer to a constant
This is the mix of previous declarations. All (restrictive) rules described above are valid. It means you can’t change the pointer and you can’t change the pointed value.
Notes
The
constmodifier is not restricted to be used to pointers and functions return value. For example:Remember that the
constmodifier can always be removed using a C-style cast or aconst_cast.Conclusions
So, going back to your question, is it useful a function with a
const intreturn value?If it’s a pointer my answer is yes, even if it can be removed with a cast: the purpose of
constis to communicate intentions (saving you from stupid errors hard to find) so more your function communicates and better it’ll be used. Updating this from your last example my answer is yes, use const wherever appliable. Who will call your code will thank you (and you’ll do it with yourself).If it’s just a
const int(or another primitive type) it may be very rare you have to write something like that. Very often they’re just intermediate results of a long calculation then it’s useless to declare them asconst(primitive types won’t change but will be combined to create a new value). Note, however, that if it make some sense to declare them asconstthen you should do it. I guess it’s more common to see a constant primitive type declared as local variable (again to be sure it won’t be changed by mistake). For example:Or in functions, why not? If you always use this rule (
constif shouldn’t be changed) you’ll see on-the-fly when a parameter isn’tconstthen you’ll understand you’ll modify it somewhere (maybe with a small, hidden, pretty++at the most right character of your screen).For objects I think it’s even more common to use the const modifier (because a
const Treturn type makes sense very often), for example:Topic isn’t ended, of course, because of aliases, mutables, operators overloading and the differences between C and C++…