What Should I Return?

closed account (zb0S216C)
I've got a method that supposed to yield the index of a map entry that has the requested tag. The index is of type unsigned int. Since unsigned types cannot have a value lower than zero, and exceptions are slow, I don't know what to return if the method cannot find the tag.

Here's my method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template< typename Obj_Type, typename Tag_Type >
const UInt &Object_List< Obj_Type, Tag_Type >::Query_Index_of_Tag( const Tag_Type &Tag ) const
{
    if( !this->Buffer_Start_ )
        throw( Exp_Access_Violation );

    // Locate the tag...
    const List_Object *Pos( this->Buffer_Start_ );
    while( Pos <= this->Last_Obj_ )
    {
        if( Pos->Tag == Tag )
            return( this->Buffer_Start_ + Pos );

        else ++Pos;
    }

    // Failed to locate the tag.
    return( ??? );
}

What do I return? How do I handle this?

Thanks.

Wazzak
You could change the signature:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template< typename Obj_Type, typename Tag_Type >
bool Object_List< Obj_Type, Tag_Type >::Query_Index_of_Tag( const Tag_Type &Tag, UInt &ret) const
{
    if( !this->Buffer_Start_ )
        throw( Exp_Access_Violation );

    // Locate the tag...
    const List_Object *Pos( this->Buffer_Start_ );
    while( Pos <= this->Last_Obj_ )
    {
        if( Pos->Tag == Tag )
        {
            ret = this->Buffer_Start_ + Pos;
            return true;
        }
        ++Pos;
    }

    // Failed to locate the tag.
    return false;
}
closed account (zb0S216C)
Hmmmm.... It maybe an additional 4 byte push, but I can bend to that. Thanks KBW :)

Wazzak
First of all the code is not good. For example there are names Object_List and List_Object in the function. What is the difference between these two entities?

As for your question if you maintain total number of elements in your container you can return it in case of failure.
Last edited on
closed account (zb0S216C)
vlad from moscow wrote:
First of all the code is not good.

Then show me an efficient method that locates a given identifier within a list.

vlad from moscow wrote:
For example there are names Object_List and List_Object in the function. What is the difference between these two entities?

It's kinda obvious. Object_List is a list of objects. List_Object is an object within the list.

vlad from moscow wrote:
As for your question if you maintain total number of elements in your container you can return it in case of failure.

I do keep track of the amount of objects within the list, with a pointer. Besides, the caller can easily mistake the size for an offset.

Wazzak
Last edited on
You could do what kbw suggested, what vlad has suggested or you could do what the standard library does with iteratorscontainers and return a pair. See std::map insert

1
2
pair.first; //Could be the index
pair.second; //A bool, true when the index found, false when it wasn't found. 


Then you can truly decide what index is returned as in the last one, or last + 1 etc. The caller can check with pair.second.
Last edited on
Topic archived. No new replies allowed.