get classtype from std::list?

Started by
7 comments, last by Washu 11 years, 11 months ago
Hi
I got a macro that i love smile.png 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
Advertisement
This seems like a horrible macro. Especially since you're unable to iterate over anything except std::list.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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;
BOOST_FOREACH does this, better. C++11 has a 'native' alternative.
[TheUnbeliever]
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
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.
f@dzhttp://festini.device-zero.de

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++.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


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

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement