By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

Avoiding a reference to a reference using iostream

P: n/a
Hi everyone,

I'm looking for some help migrating my code from Microsoft VC++ 6.0,
which was not the most conformant compiler I have now discovered. I'm
trying to move to .NET and I've made the obvious changes to my functions
so they now all take iostreams by reference rather than value to avoid
calling the private copy constructor.

There is one piece of logging code I cannot get working however, so I'm
looking for some help.

I'm having problems with taking a reference to a reference as
demonstrated by the following bit of code. I know the problem lies
in the fact that the function I am passing in to bind2nd takes a
reference to an iostream as its second argument. All the solutions to
the reference-to-a-reference problem I have found on google and in the
archives of this group only work to strip the reference of an existing
object. As the argument of the function is not an object, I cannot use
these so I am stumped.

Has anyone got any idea how I can get this code to work without having
to resort to using VC++ 6.0 again?

Cheers,
Craig Nicol.

/// test_reference.cpp follows ///

#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <functional>

struct print_pair : public binary_function<pair<int,int>,ostream&,void>
{
void operator()(pair<int,int> item, ostream& out)
{
out << "'" << item.first << ", " << item.second << "'\t";
};
} printme;

int main(int argc, char *argv[])
{
vector<pair<int, int> > twointvec;
pair<int, int> twoint;

for(int i = 0; i < 100; i += 2)
{
twoint = pair<int,int>(i, i+1);
twointvec.push_back(dint);
}

printme(twointvec[0], cout);

for_each(twointvec.begin(),
twointvec.end(),
bind2nd(printme,cout));

cout << endl;

return 0;
}

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Craig Nicol <ma**********@another.NoSPAM.cSPAMom> wrote in
news:GX**************@newsfe1-gui.server.ntli.net:
Hi everyone,

I'm looking for some help migrating my code from Microsoft VC++ 6.0,
which was not the most conformant compiler I have now discovered. I'm
trying to move to .NET and I've made the obvious changes to my
functions so they now all take iostreams by reference rather than
value to avoid calling the private copy constructor.

There is one piece of logging code I cannot get working however, so
I'm looking for some help.

I'm having problems with taking a reference to a reference as
demonstrated by the following bit of code. I know the problem lies
in the fact that the function I am passing in to bind2nd takes a
reference to an iostream as its second argument. All the solutions to
the reference-to-a-reference problem I have found on google and in the
archives of this group only work to strip the reference of an existing
object. As the argument of the function is not an object, I cannot use
these so I am stumped.


(...)

Isn't bindXth using a reference-to-const to hold function argument?
It pretty much rules out non-const reference arguments and side effects.

On the other hand, you can wrap the reference in an object which itself
will be const and passed by value. See the following code:

#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

// simple reference wrapper
template <class T>
struct by_ref {
by_ref(T& ref) : ref(ref) { }
T& get() const { return ref; }
private:
T& ref;
};

// note: by_ref<ostream> used instead of ostream&
struct print_pair
: public binary_function<pair<int,int>,by_ref<ostream>,void >
{
void operator()(pair<int,int> item, by_ref<ostream> out) const
{
out.get() << "'" << item.first << ", " << item.second << "'\t";
};
} printme;

int main(int argc, char *argv[])
{
vector<pair<int, int> > twointvec;
pair<int, int> twoint;

for(int i = 0; i < 100; i += 2)
{
twoint = pair<int,int>(i, i+1);
twointvec.push_back(twoint);
}

printme(twointvec[0], cout);

// instead of passing ostream& directly, use a reference wrapper
for_each(twointvec.begin(),
twointvec.end(),
bind2nd(printme,by_ref<ostream>(cout)));

cout << endl;

return 0;
}

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #2

P: n/a
bartek wrote:
Craig Nicol <ma**********@another.NoSPAM.cSPAMom> wrote in
news:GX**************@newsfe1-gui.server.ntli.net:

Hi everyone,

I'm looking for some help migrating my code from Microsoft VC++ 6.0,
which was not the most conformant compiler I have now discovered. I'm
trying to move to .NET and I've made the obvious changes to my
functions so they now all take iostreams by reference rather than
value to avoid calling the private copy constructor.

There is one piece of logging code I cannot get working however, so
I'm looking for some help.

I'm having problems with taking a reference to a reference as
demonstrated by the following bit of code. I know the problem lies
in the fact that the function I am passing in to bind2nd takes a
reference to an iostream as its second argument. All the solutions to
the reference-to-a-reference problem I have found on google and in the
archives of this group only work to strip the reference of an existing
object. As the argument of the function is not an object, I cannot use
these so I am stumped.

(...)

Isn't bindXth using a reference-to-const to hold function argument?
It pretty much rules out non-const reference arguments and side effects.

On the other hand, you can wrap the reference in an object which itself
will be const and passed by value. See the following code:

#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

// simple reference wrapper
template <class T>
struct by_ref {
by_ref(T& ref) : ref(ref) { }
T& get() const { return ref; }
private:
T& ref;
};

// note: by_ref<ostream> used instead of ostream&
struct print_pair
: public binary_function<pair<int,int>,by_ref<ostream>,void >
{
void operator()(pair<int,int> item, by_ref<ostream> out) const
{
out.get() << "'" << item.first << ", " << item.second << "'\t";
};
} printme;

int main(int argc, char *argv[])
{
vector<pair<int, int> > twointvec;
pair<int, int> twoint;

for(int i = 0; i < 100; i += 2)
{
twoint = pair<int,int>(i, i+1);
twointvec.push_back(twoint);
}

printme(twointvec[0], cout);

// instead of passing ostream& directly, use a reference wrapper
for_each(twointvec.begin(),
twointvec.end(),
bind2nd(printme,by_ref<ostream>(cout)));

cout << endl;

return 0;
}

Works like a dream. Thanks bartek. You've saved me an awful lot of
hair-pulling.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.