Writing a wrapper for std::list...

Started by
14 comments, last by Zahlman 16 years, 2 months ago
I've been writing wrappers for some of the std data types in C++. All has gone well for std::vector, std::map, but I'm having trouble dealing with std::list::iterators:

#include <list>

template <class T>
class List {

public:

    /* This part will not compile unless I give an exact
       Type instead of 'T' */
    std::list<T>::iterator insert();


private:

    std::list<T> _list;

};

As I wrote in the source above. I'm having trouble getting std::list<T>::iterator to compile... It compiles fine with std::list<float>::iterator, it is just the template type that is throwing it off. Any ideas?
Advertisement
What is the error message? It doesn't, perchance, mention typename, does it?
Quote:Original post by c_olin
I've been writing wrappers for some of the std data types in C++. All has gone well for std::vector, std::map, but I'm having trouble dealing with std::list::iterators:


Why on earth would you do such a thing?! Do you also varnish your windows?

Quote:I'm having trouble getting std::list<T>::iterator to compile... It compiles fine with std::list<float>::iterator, it is just the template type that is throwing it off.


Always give the compiler error. This response is a shot in the dark.

The compiler will assume that std::list<T>::iterator is not the name of a type unless you tell it so:

// ...typename std::list<T>::iterator insert();// ...


or, ideally:

// ...typedef typename std::list<T>::iterator iterator;// ...iterator insert();// ...


Even more ideally, just use std::list as is.
Sounds like a tremendous waste of time. If you post your wrapper, we could critique your choices.

The reason why std::list and friends even exists is so that every C++ programmer who needs to use common data structures (and algorithms) can do so in any program. The alternative is something like in C, where every program often has these re-implemented, with varying APIs. Hence the important word 'standard'.
I too question why you're writing these wrappers. If you seek a deeper understanding of the underlying container, try rolling your own. If you just want an easy to use, pre-existing container, what's wrong with using a vanilla std::list/vector/whatever, such that you need to wrap it yourself?
Mostly because I do plan in the future to write my own implementations of these structures... and also for cleanliness.

Thanks for the help.
Quote:Original post by c_olin
Mostly because I do plan in the future to write my own implementations of these structures... and also for cleanliness.

Thanks for the help.


Writing your own implementation for learning purposes while trying to conform to the (tried and tested) API of std::list would be a more admirable task, IMO. Don't forget to post your implementations here for comments/problems!
If you just want to make room for the possibility that you'll use your own list class in the future, just use typedefs; you don't need to write wrapper classes.
One of the main reasons why I have been making these "wrapper" classes is to include functions that the standard library does not contain and that I need to do a lot.

For example. I'm not using smart pointers so I needed a function to loop through and delete all pointers in a given list/map/vector. So this saves me a lot of code. It also makes my code much more readable (to me) as I really dislike the std library naming conventions. Maybe "wrapper" isn't the correct terminology.

Rephrase: I'm writing list/vector/map classes extending upon the std libraries versions.

But thanks for the help you guys. I got it to compile.
Quote:Original post by c_olin
For example. I'm not using smart pointers so I needed a function to loop through and delete all pointers in a given list/map/vector.
For what it's worth, Boost has a set of 'pointer container' classes that offer functionality similar to what you describe above. (Of course, they follow the standard library naming conventions, and so might not be to your liking :)

This topic is closed to new replies.

Advertisement