Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"02-23-11 - Some little coder things - Loop"

8 Comments -

1 – 8 of 8
Blogger dfan said...

Mine is

#define FOR_EACH( it, coll ) for (coll##_TYPE::iterator it = coll.begin(); it != coll.end(); ++it)

and then I can just type e.g.

FOR_EACH( it, mNotes ) { (*it)->Play(); }

I then do have to #define a foo_TYPE for each collection foo, but since I am generally only doing this for member variables, it's not so bad.

This is the only way I can bear to deal with STL iterators rather than indices.

February 24, 2011 at 7:13 AM

Blogger cbloom said...

"#define FOR_EACH( it, coll ) for (coll##_TYPE::iterator it = coll.begin(); it != coll.end(); ++it)"

Hmm.. the need for _TYPE types without typeof or auto types.

It will be much nicer in C++0x

February 24, 2011 at 10:18 AM

Blogger cbloom said...

I should also note that the disadvantages of this sort of meta-language macroing are many. It makes code harder to share. It makes code harder for people who aren't in your system to read. It makes debugging harder if there's ever a problem in the macro. It can create bugs due to the macro not doing exactly what's expected. etc.

February 24, 2011 at 11:17 AM

Blogger Tom Forsyth said...

Interesting that you put the "for" outside the macro. Does this help something non-obvious, or is it just a stylistic thing?

February 24, 2011 at 4:17 PM

Blogger cbloom said...

It's just stylistic. In the previous discussion someone suggested it. I do think there is merit in leaving the flow control indicators outside of the macros.

February 24, 2011 at 11:44 PM

Blogger Jaba Adams said...

For some more inspiration, or perhaps brain-meltage, check out Common Lisp's LOOP macro / mini-language:

http://www.gigamonkeys.com/book/loop-for-black-belts.html

February 27, 2011 at 8:56 PM

Blogger jeskola said...

#undef

March 9, 2011 at 5:30 AM

Blogger cbloom said...

LOL, well played.

March 9, 2011 at 8:52 AM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.