Passing a function pointer as template parameter
<p>For educational purposes I am trying to write my own "ForEach" function:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;


//
// This works
//
template&lt;class Container&gt;
void ForEach_v1(const Container &amp; inContainer, void (*Functor)(const std::string &amp;))
{
    typename Container::const_iterator it = inContainer.begin(), end = inContainer.end();
    for (; it != end; ++it)
    {
        Functor(*it);
    }
}


//
// Does not work
//
template&lt;class Container, class Functor&gt;
void ForEach_v2(const Container &amp; inContainer, Functor inFunctor)
{
    typename Container::const_iterator it = inContainer.begin(), end = inContainer.end();
    for (; it != end; ++it)
    {
        Functor(*it);
    }
}

void PrintWord(const std::string &amp; inMessage)
{
    std::cout &lt;&lt; inMessage &lt;&lt; std::endl;
}

int main()
{
    std::vector&lt;std::string&gt; words;
    words.push_back("one");
    words.push_back("two");
    words.push_back("three");

    // Works fine.
    std::cout &lt;&lt; "v1" &lt;&lt; std::endl;
    ForEach_v1(words, PrintWord);

    // Doesn't work.
    std::cout &lt;&lt; "v2" &lt;&lt; std::endl;
    ForEach_v2(words, PrintWord);

    return 0;
}
</code></pre>

<p>The compiler output:</p>

<pre><code>|| g++ -Wall -o test main.cpp
|| main.cpp: In function 'void ForEach_v2(const Container&amp;, Functor) [with Container = std::vector&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;, Functor = void (*)(const std::string&amp;)]':
main.cpp|116| instantiated from here
main.cpp|96| warning: unused variable 'it'
</code></pre>

<p>Program output:</p>

<pre><code>v1
one
two
three
v2
</code></pre>

<p>My questions:</p>

<ul>
<li>Why doesn't <code>ForEach_v2</code> print anything?</li>
<li>Why does the compiler print the "unused variable"-warning for <code>ForEach_v2</code>?</li>
</ul>
