Recommended Posts

I would like to know if it''s possible to pass the name of a member function as a template argument. The case is the following : I''m creating a single-linked list that has some additional features with regards to std::slist, and I would like to have a templated "map" function which receives as a template argument the member function of the stored object that is to be called. For instance, a call to map<thisFunction> would call Object.ThisFunction( ) on all objects.
For simplicity''s sake, I assume all such argument functions are of type void (void).

Should also work if you expect this map functino to allways use the same function name... If you planned on taking a type of function in numerous places, creating a common typedef would shorten your typing. Aka:

Share this post

Link to post

Share on other sites

The problem with std::for_each is that I don''t have any iterators to work with, only an internal linked list structure.

Also, if you wonder why I''m creating my own slist instead of using the stl one (and I know this sound like reinventing the wheel) I currently need to be able to memcopy the list to a file, or vice versa, during the save process. This means I cannot rely on the stl implementations because1°/ Compilation will be done using three different compilers for three different platforms, but save files must be exchangeable between the platforms.2°/ I need to run through all pointers and convert them to a save/loadable state, including the pointers used internally by the SList

And also...3°/ I know at compile-time the maximum number of objects in the list, but I''m sure this one can be solved using STL.

Share this post

Link to post

Share on other sites

Writing a forward iterator class for a singly linked list is easy. As data all you need is a pointer to your node and then overload operator++() to get the next node. Once you have a compatible forward iterator you should be able to use std::for_each() on your linked list class just fine.