If I have code such as
class CString { int GetLength(); };
bool smaller(CString s1, std::string s2) {
return s2.size() > s1.GetLength();
}
What is the best thing for me to do?
Change
s1.GetLength()
to(size_t)c.GetLength()
?
This would get help get rid of a compiler warning regarding "signed-unsigned mismatch", and communicate my intention to cast, and is by far the easiest route. But it's probably frowned upon. :(Change
s1.GetLength()
tostatic_cast<size_t>(c.GetLength())
?
This would get help get rid of the warning, with "The Correct" kind of cast.Change
s1.GetLength()
tostatic_cast<std::string::size_type>(c.GetLength())
?
It's extremely verbose... is there a practical benefit to this abstraction, or should I break it?Leave it as is?
This would help make the compiler do overflow-checking using the/RTCc
switch (my main concern here), at the expense of a warning.Do something else?
Should I make my own casting function? Use a macro? Should I check at run-time as well as compile-time? Any other ideas?
Edit:
It seems like the example is being taken a little too literally...
I obviously didn't mean to talk just about CString::GetLength()
. That particular method is certainly not a huge worry of mine. :) What I am worried about is the more general case, of when I'm getting an integer that's never supposed to be negative, but which could theoretically be, due to bugs.
Heck, I might be writing a method that does this, in order to override another piece of code -- so I can't change the signature. And my code could certainly have bugs, even though I wouldn't expect it.
In such a case, what should I do?