Is there a syntax for merging the following 2 functions into one, so that if input is const
, result is const
, and if input is non-const, result is non-const?
char* f1(char* x) {
return x+1;
}
const char* f2(const char* x) {
return x+1;
}
This
Does not make it "one function", but it is one set of overloads and for the user it shouldnt matter that it actually is more than one function unless they try to take the address.
There are ways to avoid duplication of the implementation, perhaps the most simple is to implement the non-const overload in terms of the const one:
In the second overload
x
points to a non-constchar
, hence it is ok to cast away the constness from result of the other overload.