Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Trouble passing a function template into "for_each" algorithm

Reply
Thread Tools

Trouble passing a function template into "for_each" algorithm

 
 
Griff
Guest
Posts: n/a
 
      07-10-2004
#include <iostream>
using namespace std;

#include <vector>
#include <string>
#include <fstream>
#include <algorithm>

template<class C>void PrintAll(C&v)
{
typename C::iterator i;
for (i = v.begin(); i!= v.end(); ++i)
{
cout << *i << endl;
}
}

void main()
{
const string s1 = "First line";
const string s2 = "Second line";
const string s3 = "Third line";

vector<string> V;

V.push_back(s1); V.push_back(s2); V.push_back(s3);

for_each(V.begin(),V.end(),PrintAll);
}

================================================== ===========
I won't bore you (yet) with the reams of complaint from MS VC++ when I
try to compile the above...

My question is:

What is the correct syntax to use "for_each" to call the function
template PrintAll ?

for_each(V.begin(),V.end(),PrintAll<const string>); ??
for_each(V.begin(),V.end(),PrintAll); ??

or is it even possible ?

Assistance and explanation would be welcomed on this.

Best regards,

Griff
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-10-2004
"Griff" <(E-Mail Removed)> wrote...
> #include <iostream>
> using namespace std;
>
> #include <vector>
> #include <string>
> #include <fstream>
> #include <algorithm>
>
> template<class C>void PrintAll(C&v)
> {
> typename C::iterator i;
> for (i = v.begin(); i!= v.end(); ++i)
> {
> cout << *i << endl;
> }
> }
>
> void main()


int main()

> {
> const string s1 = "First line";
> const string s2 = "Second line";
> const string s3 = "Third line";
>
> vector<string> V;
>
> V.push_back(s1); V.push_back(s2); V.push_back(s3);
>
> for_each(V.begin(),V.end(),PrintAll);


for_each(V.begin(), V.end(), PrintAll<string>);

> }
>
> ================================================== ===========
> I won't bore you (yet) with the reams of complaint from MS VC++ when I
> try to compile the above...
>
> My question is:
>
> What is the correct syntax to use "for_each" to call the function
> template PrintAll ?
>
> for_each(V.begin(),V.end(),PrintAll<const string>); ??
> for_each(V.begin(),V.end(),PrintAll); ??


Since 'PrintAll' is a template, and 'for_each' needs a function or
a functor, you need to _instantiate_ it: PrintAll<string>.

>
> or is it even possible ?


Of course it is.

C++ Standard library's "string" class conforms to the Standard Container
requirements and that means it has the "iterator" type defined, has
"begin" and "end" members, so you can use it to iterate the characters
in it.

As to Visual C++, I don't know whether 6.0 will be able to compile this,
but I sincerely hope that 7.1 will.

Victor


 
Reply With Quote
 
 
 
 
Griff
Guest
Posts: n/a
 
      07-11-2004
Victor,

Thanks for that. Your solution was pretty much what I tried, but VC++
6.0 chokes on it. Glad to have independent confirmation that the
problem is with MS,
and not my understanding of the code (for once).

I'll try it again at work on monday, when I can use a decent system.
(Solaris and g++).

Cheers

- Griff
 
Reply With Quote
 
Griff
Guest
Posts: n/a
 
      07-12-2004
>
> I'll try it again at work on monday, when I can use a decent system.
> (Solaris and g++).
>


Yep, works beautifully on Solaris 8 with gcc 3.2...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM
Passing template struct as an argument to a template function nifsmith C++ 5 09-27-2004 02:35 PM
function non-template versus fully specialized function template Robert Allan Schwartz C++ 1 08-09-2004 03:41 PM



Advertisments