working with g++ implementation of the STL

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

working with g++ implementation of the STL

David Arnstein
I am hitting a problem with std::list and std::map. In this post, I
have a simple (I hope) example of this.

I attempted this template function, whose purpose is to search a list,
where "list" is one of the standard containers in the C++ STL
(standard template library).

template <typename T>
std::list::const_iterator<T> xfind (
    const std::list<T>& my_list, const T& candidate)
{
    std::list<T>::const_iterator list_iterator;
    for (list_iterator = my_list.begin() ; list_iterator != my_list.end() ;
         ++list_iterator)
    {
        if (*list_iterator == candidate) break;
    }
    return list_iterator;
}

Fail! the compiler barfs on the line
        std::list::const_iterator<T> xfind (
It complains
        parse_register_txt.cc:32: error: 'template<class _Tp, class _Alloc> class std::list' used without template parameters
        parse_register_txt.cc:32: error: expected constructor, destructor, or type conversion before '<' token

I think I know what the problem is. The g++ implementation of "list"
includes this:

  template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
    class list : protected _List_base<_Tp, _Alloc>
  {
    .
    .
    .
    typedef _List_const_iterator<_Tp>                  const_iterator;
    .
    .
    .
  };

In other words, std::list::const_iterator is implemented as an entity
_List_const_iterator that is in the std namespace. Using this insight,
I can get the compiler to accept this:

template <typename T>
std::_List_const_iterator<T> xfind (
    const std::list<T>& my_list, const T& candidate)
{
    std::_List_const_iterator<T> list_iterator;
    for (list_iterator = my_list.begin() ; list_iterator != my_list.end() ;
         ++list_iterator)
    {
        if (*list_iterator == candidate) break;
    }
    return list_iterator;
}

I find this to be unsatisfying. I am concerned that _List_const_iterator
may be a non-portable feature of gcc.

Is there a better way?
--
David Arnstein                       (00)
[hidden email]          {{   }}
                                     ^^
_______________________________________________
help-gplusplus mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus