I am hoping to clear up some confusion here. This seems like a simple one but I can't find a clear answer.
Should I always use the explicit return type of a function unless I have a good reason not to (eg. a need to conserve memory), even if I know the range of what can be returned will fit in a smaller type?
Using the following function as an example:
size_t std::string::find_last_of (char c, size_t pos = npos) const;
Is this:
std::string s("woof.woof.meo.w");
size_t result = s.find_last_of('.');
Less, equally, or more efficient than this:
std::string s("woof.woof.meo.w");
unsigned char result = s.find_last_of('.');
I think a size_t is constructed initially no matter what type the result is copied to. So there is no performance gain when implicitly casting to a smaller type. But is there a performance hit? What happens with the redundant bits from the larger type?
Thanks for your time and I appreciate your guidance.