Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Boost: Any gotchas with BOOST_FOREACH?

Tags:

This one is for Boost experts. Are there any gotchas or details that the programmer needs to be aware of before he goes in and replaces all his old C/C++ style loops with the lean-and-mean-looking BOOST_FOREACH?

(This question is partly derived from here.)

like image 928
Ashwin Nanjappa Avatar asked Apr 04 '09 10:04

Ashwin Nanjappa


2 Answers

Take a look at:

  • The Conditional Love article for an understanding of the rationale
  • The Pitfalls section of documentation
  • The Portability section just in case you are developing cross-platform products
  • The bugs page for BOOST_FOREACH
like image 67
dirkgently Avatar answered Sep 20 '22 15:09

dirkgently


BOOST_FOREACH - macro, I don't like macroses and prefer to use STL algorithms + lambda + bind.

Also C++0x will contain for-loop similar on BOOST_FOREACH:

int my_array[5] = {1, 2, 3, 4, 5}; for(int &x : my_array) {   x *= 2; } 

it is one additional reason for don't use partialy dead BOOST_FOREACH.

like image 43
bayda Avatar answered Sep 21 '22 15:09

bayda