Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. i have a question about for_each

i have a question about for_each

Scheduled Pinned Locked Moved ATL / WTL / STL
wpfgraphicsquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lxlenovostar
    wrote on last edited by
    #1

    why can i code like this:

    #include #include #include #include using namespace std;

    template void OUT(T c)
    {
    cout << "!!" << c ;
    }

    int main()
    {
    vector st(10, " ");

        for\_each(st.begin(), st.end(), OUT);
    

    }

    i know information about for_each : /*template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function f); Apply function to range Applies function f to each of the elements in the range [first,last).*/ but why they don't use Function Templates instead of function, thank you

    A X 2 Replies Last reply
    0
    • L lxlenovostar

      why can i code like this:

      #include #include #include #include using namespace std;

      template void OUT(T c)
      {
      cout << "!!" << c ;
      }

      int main()
      {
      vector st(10, " ");

          for\_each(st.begin(), st.end(), OUT);
      

      }

      i know information about for_each : /*template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function f); Apply function to range Applies function f to each of the elements in the range [first,last).*/ but why they don't use Function Templates instead of function, thank you

      A Offline
      A Offline
      Alain Rist
      wrote on last edited by
      #2

      Not sure to answer your question as it is formatted but f can either be a pointer to a function or an object whose class overloads operator()(InputIterator::value_type). See http://www.cplusplus.com/reference/algorithm/for_each/[^] cheers, AR

      When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

      1 Reply Last reply
      0
      • L lxlenovostar

        why can i code like this:

        #include #include #include #include using namespace std;

        template void OUT(T c)
        {
        cout << "!!" << c ;
        }

        int main()
        {
        vector st(10, " ");

            for\_each(st.begin(), st.end(), OUT);
        

        }

        i know information about for_each : /*template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function f); Apply function to range Applies function f to each of the elements in the range [first,last).*/ but why they don't use Function Templates instead of function, thank you

        X Offline
        X Offline
        xtofl
        wrote on last edited by
        #3

        This is called 'generic' programming. The term 'generic' means that the definition of the for_each function does not assume anything else from the template argument than that it has an operator()( T ). If they would have implemented it using a function pointer, # they would have needed to specify the function pointer's type (e.g. void (*) (int)) and thus would have disallowed any other type of function (remember C) # they would have disallowed the use of stateful functions (e.g. accumulators), like often needed for the algorithms Also, the usage of templates greatly improves the optimizer's chances: it knows _at compile time_ what the body of the for_each block is going to be, so it can often skip parameter pushing etc..., hence reducing function call overhead.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups