Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a overloaded function as a template

I have this template set

        __Self &set(const char *name, lua_CFunction func)
        { return rawSet(name, FuncCall::create(func)); }
....

that i use like:

.set("child_value", &pugi::xml_node::child_value)

But child_value is overloaded with

const char_t* xml_node::child_value(const char_t* name) const
const char_t* xml_node::child_value() const

and the compiler is giving this error:

error C2668: 'SLB::Class<T,W>::set' : ambiguous call to overloaded function

How could i fix this error ? I want the child_value() version.

like image 922
bratao Avatar asked Jun 12 '26 10:06

bratao


2 Answers

Define typedefs as:

typedef const char_t* (pugi::xml_node::*fn_pchar)(const char_t* name) const;
typedef const char_t* (pugi::xml_node::*fn_void)() const;

And then write:

//if you want to select first member function that takes parameter (char*)
set("child_value", (fn_pchar)&pugi::xml_node::child_value); 
                  //^^^^^^^^ note this!

//if you want to select second member function that takes no parameter (void)
set("child_value", (fn_void)&pugi::xml_node::child_value); 
                  //^^^^^^^ note this
like image 63
Nawaz Avatar answered Jun 13 '26 23:06

Nawaz


I think an explicit cast is needed:
.set( "child_value", static_cast<const char_t* (xml_node::*)() const>( &pugi::xml_node::child_value ) );

like image 26
Eugen Constantin Dinca Avatar answered Jun 14 '26 00:06

Eugen Constantin Dinca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!