Topic: Empty vectors with legacy interface


Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/09/18
Raw View
Valentin Bonnard wrote:

> Ed Brey wrote:

> > Assuming that the DR requiring vector be stored like an array goes through,
> > I still see a potential problem using vector with legacy code if the vector
> > happens to be empty.

> I have already written two helpers functions for this purpose:
[ sniped ]

Here is another one:

// [first, last) is a valid range
// first, last are inside a vector
// returns: another range which the same content

template <class VectorIterator>
inline std::pair<T*,T*> range_ptr_from_iter (VectorIterator first,
                                             VectorIterator last)
{
    size_t diff = last - first;
    return diff ? std::pair<T*,T*> (&*first, &*first + diff)
                : std::pair<T*,T*> (NULL, NULL);
    is_iter_pair (first, last); // comment out if it causes problems
}

template <typename T, class A>
inline void is_iter_pair (std::vector<T,A>::const_iterator,
                          std::vector<T,A>::const_iterator)
{}

template <typename T, class A>
inline void is_iter_pair (std::vector<T,A>::iterator,
                          std::vector<T,A>::iterator)
{}

template <typename T>
inline std::pair<T*,T*> range_ptr_from_iter (T* first, T* last)
{
    return std::pair<T*,T*> (first, last);
}

The problem is that I can't write a function to convert a
single iterator into a vector to a pointer.

std::vector::iterator::base (), anyone ?

--

Valentin Bonnard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]