• Advertisement
Sign in to follow this  

Writing a wrapper for std::list...

This topic is 3739 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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'.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Mostly because I do plan in the future to write my own implementations of these structures... and also for cleanliness.

Thanks for the help.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement