[C++] Operate on std::list of arbitrary type?

Recommended Posts

Hi!
Is it possible to write a function that accepts any std::list as an argument, regardless of the type of its elements?
If not, what are the alternatives?
Any sort of help is very welcome. =)

Share on other sites
You can use templates:
template<typename T>void function(const std::list<T> &list) {    // ...    // You may need to use typename for nested types, like iterators}

Now, the C++ way to do this is to accept a pair of templated iterator types. This way it will work with (almost) any C++ standard container, and others that conform to its interface. It also allows you to work with sub-ranges.
template<typename Iterator>void function(Iterator begin, Iterator end) {    // ...}

Share on other sites
If I'm not mistaken, the first suggestion would leave me with no ability to iterate through my list and the second one would leave me with no ability to insert\erase elements. My function needs to be able to do both. I guess I could combine the two by using two template parameters. Well, I'm clearly no C++ expert... is that an acceptable solution?

Share on other sites
Then pass a non-const reference rather than a const reference.

Share on other sites
you can iterate over the list and modify it
template <class T>void func(std::list<T>& l, T val){	//example of usage, 	for(std::list<T>::iterator it = l.begin(); it != l.end(); it++)	{           l.insert(it, val);	}}

Share on other sites
Thanks for the replies, guys. =)

@SiCrane: I'm not sure I follow... Could I have a snippet of example code, please?
@blubberbert: That would be nice, but, as rip-off hints in his post, it won't compile.

Share on other sites
Quote:
 Original post by StasB@blubberbert: That would be nice, but, as rip-off hints in his post, it won't compile.

for (typename std::list<T>::iterator it = l.begin(); ...

Share on other sites
Ah, yes, that one works! I guess I have some reading to do on the subject of templates. =)
Thank you, guys.
On a slightly unrelated note... When I define my template in a separate file of the project and try to include it in Main.cpp, any changes I make to the template are ignored until changes are made to Main.cpp. I guess this behavior makes sense for interface headers, but it's clearly a problem for templates. Is this something specific to Dev-Cpp? What can I do about it?

Share on other sites
That shouldn't happen. When you update a header file, any files that depend on it should be recompiled.

Maybe it has something to do with how your project is configured, or maybe it's a bug in Dev-Cpp, but you shouldn't use Dev-Cpp anyway.

Share on other sites
Heheh. People have been telling me that for years. I'm going to have a hard time switching to something else. I love this tool because it's so small and simple. Coupled with a few DevPacks on a USB drive, I could install it within minutes and start coding on any PC.

Create an account

Register a new account

• Forum Statistics

• Total Topics
627737
• Total Posts
2978873

• 10
• 10
• 21
• 14
• 12