return types

Pages: 12
@JLBorges: with std::string, it's fine because it uses a named constant. If you just use -1, however, there can easily be some discrepancies concerning sign mismatch in comparisons and also converting to different size unsigned types. I've also been told that std::string's functions have that interface because they were designed before things like iterators were added to the language:
http://stackoverflow.com/q/3061966/1959975
Are you sure it's a safe/okay thing to do?
> If you just use -1, however, there can easily be some discrepancies
> concerning sign mismatch in comparisons

1
2
3
4
5
6
unsigned int someFunc(vector<items*> item)
{
    // ...
    
    return -1 ; // -1 converted to unsigned int
}


It has already been converted to unsigned int when the function returns.
The only comparison that the caller is expected to perform is an an equaliy / inequality comparison.

1
2
3
4
5
6
7
8
9
10
11
const auto pos = someFumc( some_vector ) ;

if( pos != -1 ) // -1 converted to unsigned int (usual Arithmetic Conversions)
                // http://msdn.microsoft.com/en-us/library/3t4w2bkb.aspx
{
      // access some_vector[pos] 
}
else
{
     // not found
}



> I've also been told that std::string's functions have that interface
> because they were designed before things like iterators were added to the language

No.

Even in C++98, the string class did provide iterators; had constructors which took polymorphic iterators; member functions insert/erase/append had overloads that took polymorphic iterators etc.
Topic archived. No new replies allowed.
Pages: 12