Specifying address of const overloaded function

N

Noah Roberts

I am trying to keep a vector of tuples sorted based on the first member
and so for my insert function I am calling std::lower_bound with a bound
comparator like the following:

std::lower_bound(sit, eit, t, boost::bind(tuple_t::get<0>(), _1) <
boost::bind(tuple_t::get<0>(), _2));

The problem is that get<> is overloaded via const. There are the two
definitions:

const T& get<int I>() const;
T& get<int I>();

The compiler complains that my specification is ambiguous

I'm pretty sure that the const version is the correct one but I don't
know how to specify that in my attempt to bind this function. I've
tried some variations of the following theme and gotten nowhere. As I
suspect, it's a syntax error:


std::lower_bound(sit, eit, t, boost::bind(tuple_t::get<0>() const, _1) <
boost::bind(tuple_t::get<0>() const, _2));

I also tried another typedef:

typedef tuple_t const tuple_ct;

std::lower_bound(sit, eit, t, boost::bind(tuple_ct::get<0>() const, _1)
< boost::bind(tuple_ct::get<0>() const, _2));

I still get an ambiguous call error!
 
N

Noah Roberts

Ok, I have to move on to another problem so I created a little function
that simply calls the function instead of trying to bind to its address.
I'm stumped as to how to solve the original problem and I would very
much like to know how to do this. Google hasn't turned up anything of
use and I tried looking in the standard for anything appropriate but
couldn't find it. I'm at a loss. If anyone knows, please explain.
 
P

Pete Becker

Ok, I have to move on to another problem so I created a little function
that simply calls the function instead of trying to bind to its
address. I'm stumped as to how to solve the original problem and I
would very much like to know how to do this. Google hasn't turned up
anything of use and I tried looking in the standard for anything
appropriate but couldn't find it. I'm at a loss. If anyone knows,
please explain.

To take the address of an overloaded function you use a cast to select
the one you want, or you assign it directly to a variable of the
corresponding type.
 
N

Noah Roberts

Noah said:
I am trying to keep a vector of tuples sorted based on the first member
and so for my insert function I am calling std::lower_bound with a bound
comparator like the following:

std::lower_bound(sit, eit, t, boost::bind(tuple_t::get<0>(), _1) <
boost::bind(tuple_t::get<0>(), _2));

The problem is that get<> is overloaded via const. There are the two
definitions:

const T& get<int I>() const;
T& get<int I>();

The compiler complains that my specification is ambiguous

Assigning the address to a pointer of a type like so:

X_TYPE const& (tuple_t::*getter)() const = &tuple_t::get<0>;

This works. It doesn't seem possible to assign this thing to a
boost::function or bind to it directly. This monstrous cast works though:

boost::bind(static_cast<X_TYPE const& (tuple_t::*)()
const>(&tuple_t::get<0>), _2)

Personally, I prefer the assignment method...
 
R

rolkA

Ok they have answered you, but I see something strange in your code :
you are using "tuple_t::get<0>()" : to take the adress of a function,
whatever its type is, you have to remove the brackets !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top