First off I am using either MSVC++ 6.0 or MS.NET on a C++ console application. I have some code that uses the for_each function to print out some data on a set of objects using the mem_fun_ref adapter to call the object's own print function. This all works fine if the object's print function doesn't have any parameters and can internally just use a specific output stream, cout for example:
Code:
#include <set>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
class tsv
{
public:
// BTW... for .NET the function below works, but...
// MSVC++ however insists on a non-const func returning a value in the for_each call below
void print() const
{
// Output class data to cout for example
}
};
int main()
{
set<tsv> TsvSet;
// Read in multiple tsv objects and insert them into the set
// Now use for_each and tsv object's own print function for output to cout
for_each( TsvSet.begin(), TsvSet.end(), mem_fun_ref(tsv::print) ); // Works just fine
return 0;
}
What I want to have happen is to be able to have the object's print function accept a parameter to an ostream stream object so I could have my for_each function either output everything to cout for example at one point in the program or later output something to a file. I have been looking through Nicolai Josuttis' The C++ Standard Library: A Tutorial and Reference (8.2.2 Function Adapters for Member Functions) and he seems to say you can pass a single parameter to a function in this manner using bind2nd. For example:
Code:
#include <set>
#include <iostream>
#include <algorithm>
#include <functional>
#include <fstream>
using namespace std;
class tsv
{
public:
void print(ostream& out) const
{
// Output class data to "out" ostream object
}
};
int main()
{
set<tsv> TsvSet;
ofstream outfile("Output.Txt");
// Read in multiple tsv objects and insert them into the set
// Now use for_each and tsv object's own print function for output to outfile...
// This does not work for me.
for_each( TsvSet.begin(), TsvSet.end(), bind2nd(mem_fun_ref(tsv::print),outfile) );
return 0;
}
In .NET I seem to get "reference-to-a-reference" errors using the bind2nd function, in MSVC++ I think I get "cannot deduce template argument" errors. Is there some trick I need to get this working? I have tried making the parameter const/non-const, reference/non-reference, this/that and it's starting to frustrate me. Either there is something simple I'm missing or I can't do this in this fashion. I can get around this by having my print function in a zero argument manner to print to a static ofstream member of my tsv class so that the for_each function can write all the objects to the same file but I would like to know if this at least possible.