Public Group

get classtype from std::list?

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

Recommended Posts

Hi
I got a macro that i love But wanna streamline it a bit if its possible. I have

 loop(cUnit,unitList) endLoop 

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

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:

 loop(unitList) endLoop 

Thanks a lot!
Erik

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

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

Share on other sites
BOOST_FOREACH does this, better. C++11 has a 'native' alternative.

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 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 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.

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

2. Seems you still pass both class and list to the macro, or did i misunderstand?

I think he just copie'd 'n' pasted the macro. You can delete the class parameter from the macro with SiCrane's implementation.

3. Thats a separate library right? No way to do it in standard c++?

It is a separate library, but if you're using C++11, there is a way to do this in standard C++. Edited by Cornstalks

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.

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.

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.

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 on other sites

[quote name='suliman' timestamp='1336870210' post='4939672']
3. Thats a separate library right? No way to do it in standard c++?

It is a separate library, but if you're using C++11, there is a way to do this in standard C++.
[/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).

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 30
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631774
• Total Posts
3002295
×