Sign in to follow this  

get classtype from std::list?

This topic is 2045 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

Hi
I got a macro that i love [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] But wanna streamline it a bit if its possible. I have

[CODE]
loop(cUnit,unitList)

endLoop
[/CODE]

where the macro looks like:
[CODE]
#define loop(classType,myList) \
{std::list<classType *>::iterator it; \
for ( it = myList.begin(); it != myList.end(); ++it ){ \
classType * uu=*it;
[/CODE]

Any way to inside the macro get the classType so i dont have to pass it manually each time? In other words so it can be used like this:


[CODE]
loop(unitList)

endLoop
[/CODE]


Thanks a lot!
Erik

Share this post


Link to post
Share on other sites
This seems like a horrible macro. Especially since you're unable to iterate over anything except std::list.

Share this post


Link to post
Share on other sites
You could use the [tt]auto[/tt] keyword if you're using a C++11 compiler.
[code]
#define loop(classType,myList) \
for (auto it = myList.begin(); it != myList.end(); ++it ) { \
auto uu = *it;
[/code]

Share this post


Link to post
Share on other sites
1. Why is it horrible? I use it all the time and it saves me a lot of time and makes the code easier to read.
2. Seems you still pass both class and list to the macro, or did i misunderstand?
3. Thats a separate library right? No way to do it in standard c++?

Thanks
E

Share this post


Link to post
Share on other sites
The difference is that BOOST_FOREACH works for pretty much everything you can iterate over. It's not forcing you to use lists (which are an inefficient choice in most cases) nor to store pointers in your container (which often doesn't make sense). There is also no weird endLoop required and it doesn't do anything confusing like making the opening braces part of the macro or forcing a random variable name on you.

Boost being a separate library is hardly an argument. Boost is THE C++ library. Many parts of it ended up as part of C++11. It's basically the extended C++ standard library and most of it are just header files anyway.

Share this post


Link to post
Share on other sites
[quote name='suliman' timestamp='1336870210' post='4939672']
1. Why is it horrible? I use it all the time and it saves me a lot of time and makes the code easier to read.
[/quote]
1. It creates two variables it and uu, but that's just something the user has to know. There's no indication that this is what it does.
2. It's poorly named. It's not immediately clear from the name it's a foreach helper macro.
3. It only works with [font=courier new,courier,monospace]std::list[/font]s
4. It's an unnecessary macro that pollutes the global namespace (and everything else). You better hope the identifiers [font=courier new,courier,monospace]loop[/font] and [font=courier new,courier,monospace]endLoop[/font] aren't reused anywhere.
5. It can't be used with [font=courier new,courier,monospace]const std::list[/font]s (which you would probably need, if you're following const-correctness)
6. SiCrane implemented it in two lines of code. Really, it's not saving you much work.
7. My computer's battery is dying, I'll let others add to this list

[quote name='suliman' timestamp='1336870210' post='4939672']
2. Seems you still pass both class and list to the macro, or did i misunderstand?
[/quote]
I think he just copie'd 'n' pasted the macro. You can delete the class parameter from the macro with SiCrane's implementation.

[quote name='suliman' timestamp='1336870210' post='4939672']
3. Thats a separate library right? No way to do it in standard c++?
[/quote]
It is a separate library, but if you're using C++11, [url="http://en.wikipedia.org/wiki/Foreach_loop#C.2B.2B"]there is a way to do this in standard C++[/url]. Edited by Cornstalks

Share this post


Link to post
Share on other sites
[quote name='suliman' timestamp='1336870210' post='4939672']
1. Why is it horrible? I use it all the time and it saves me a lot of time and makes the code easier to read.
[/quote]
It's horrible because:
A) you're restricted to std::list, however there are a half dozen other containers you can iterate over in the C++ standard library.
B) loop, endLoop?
C) You're using using "magic variables" as your "dereferenced" type extracted from the iterator.
[quote]
2. Seems you still pass both class and list to the macro, or did i misunderstand?
[/quote]
That's because he left that portion of the macro in. Its not used anywhere and coule be removed.
[quote]
3. Thats a separate library right? No way to do it in standard c++?
[/quote]
So? Its a library that is very well written, and provides this functionality in a greatly expanded (and significantly clearer) mechanism. Use it.

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1336889218' post='4939720']
[quote name='suliman' timestamp='1336870210' post='4939672']
3. Thats a separate library right? No way to do it in standard c++?
[/quote]
It is a separate library, but if you're using C++11, [url="http://en.wikipedia.org/wiki/Foreach_loop#C.2B.2B"]there is a way to do this in standard C++[/url].
[/quote]
THat too, if he's using C++11 and Visual Studio 2011 or GCC/Clang then he can just use for(auto [const]& val : container), or in Visual Studio 2010: for each(auto [const]& val in container).

Share this post


Link to post
Share on other sites

This topic is 2045 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this