Sign in to follow this  

Pointers to overloaded functions [solved]

This topic is 4784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What's the syntax for taking the pointer to an overloaded function?
void func(float, int);
void func(double, int);

void test()
{
    std::vector<float> v1,v2;
    std::transform(v1.begin(),v1.end(),v2.begin(),boost::bind(?????,_1,2));
}




Thanks. [Edited by - joanusdmentia on November 11, 2004 6:48:49 PM]

Share this post


Link to post
Share on other sites
for a reference to a function:


static_cast<void (&)(double, int)>(func)

or

static_cast<void (&)(float, int)>(func)


for pointer to function:


static_cast<void (*)(double, int)>(func)

or

static_cast<void (*)(float, int)>(func)


I would try the reference one first with boost::bind

Share this post


Link to post
Share on other sites
Is there any way to get it to resolve the correct overload automatically? The syntax is a bit nasty.

As in something similar to when the function isn't overloaded when you could just write boost::bind(func,_1,2)

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
Is there any way to get it to resolve the correct overload automatically? The syntax is a bit nasty.


You can avoid the cast by creating a variable:


float (&func_ref)(float, int) = func;
std::transform(v1.begin(), v1.end(), v2.begin(), boost::bind(func_ref,_1,2));


EDIT: You can make it abit cleaner with some typedef'ing.

Share this post


Link to post
Share on other sites

float (&absref)(boost::math::quaternion<float> const &) = boost::math::abs;
std::transform(v1.begin(),v1.end(),v2.begin(),boost::bind(absref,_1));

Interesting, the std::transform() line generates the following errors:

...\boost\bind.hpp(62): error C2825: 'F::result_type': cannot form a qualified name
...\boost\bind.hpp(62): error C2039: 'result_type' : is not a member of 'operator``global namespace'''
...\boost\bind.hpp(62): error C2146: syntax error : missing ';' before identifier 'type'
...\boost\bind.hpp(62): error C2955: 'boost::_bi::type' : use of class template requires template argument list
...\boost\bind.hpp(62): fatal error C1903: unable to recover from previous error(s); stopping compilation

It's nothing essential, just a little test I did. Am I doing something wrong with this? I find it odd that boost::bind wouldn't be able to handle the type, it's not that complex after all.

EDIT: Hmm, reference types don't want to work, it needs to be a pointer.

Share this post


Link to post
Share on other sites

This topic is 4784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this