Pointers to overloaded functions [solved]

Started by
6 comments, last by snk_kid 19 years, 5 months ago
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]
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Advertisement
Nevermind, worked it out.
????? = (float(*)(float,int))&func
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
for a reference to a function:

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


for pointer to function:

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


I would try the reference one first with boost::bind
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)
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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.
Yeah, that's actually how I figured out what the syntax would be in the first place [smile]


Thanks.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
boost::bind doesn't like reference to functions (oh well worth a try, would have been nice), it has to be pointer to function instead, my bad.

This topic is closed to new replies.

Advertisement