   C++ for_each
  Will the C++ function for_each() allow for the function you specify to be inlined? In other words, will it generate the same code as if you had typed it all out without a function call?If I had this:   for (vector<int>::iterator i = v.begin(); i != v.end(); ++i){    // some code here}Would this for_each be the same?   inline void SomeCode(...){    // some code here}for_each(v.begin(), v.end(), SomeCode);I guess basically what I''m asking is, is for_each just syntactic sugar for what I want, or will it do more under the hood? I want something that will avoid actually calling the function, because I expect a lot of my functions will be very small.If it doesn''t do what I want, how would I go about writing one of these? I guess I could use templates, but I could use a little guidance since I don''t use templates very often. 
  yesalthough you should check the assembly output if you want to be absolutely sure that it is happening.  Remember that your function has to be defined(not just declared) at the point where you reference it with for_each if you expect it to be inlined. ;  Check this out: Herb sutter on inlining;  IIRC, this won''t be inlined:inline void SomeCode(...){  // some code here}for_each(v.begin(), v.end(), SomeCode);  But the key is to make a functor object. That way you''ll establish a type for the operation you''ll perform and the compiler can know for certain that it can be inlined:struct SomeCode {  void operator()(...) //no need for "inline" word here  {    // some code here  }};for_each(v.begin(), v.end(), SomeCode()); ;quote:Original post by Anonymous Poster   IIRC, this won't be inlined:inline void SomeCode(...){  // some code here}for_each(v.begin(), v.end(), SomeCode);    VC 7.1 inlines it   [edited by - sjelkjd on February 12, 2004 3:52:52 AM];  Yes, function objects can be inlined whereas it is highly unlikely that a function pointer will be in most cases (as sjelkjd says, the function has to be defined locally to stand a chance of being inlined).if you're checking the produced code, make sure it's optimised and not the debug version.edit: spelling sjelkjd!edit: see comments a few lines before this piece of code, Item 46 in Effective STL   [edited by - petewood on February 12, 2004 5:00:44 AM];quote:Original post by sjelkjd  VC 7.1 inlines itMy VS .NET 2003 version (the IDE) says it''s version 7.1.3088. Does that mean I have 7.1 of VC++? VC++ doesn''t give its own version number in the "About..." menu. ;  Is there any way to make sure things get inlined, like maybe by using a template function? I don''t know, something like:class thing { ... };// insert function or functor of do_something() here...vector<thing> my_things;for_each<thing, do_something>(my_things);Would that expand to the code just like if I had written it all out myself? ;  VC has __forceinline but generally programmers are not qualified to know which functions should be inlined. ;quote:Original post by sjelkjd    My VS .NET 2003 version (the IDE) says it's version 7.1.3088. Does that mean I have 7.1 of VC++? VC++ doesn't give its own version number in the "About..." menu.   VS.NET 2003 is VS 7.1.   [edited by - antareus on February 12, 2004 9:28:33 AM]