开发者

Pass a overloaded function as a template

开发者 https://www.devze.com 2023-02-26 20:41 出处:网络
I have this template set __Self &set(const char *name, lua_CFunction func) { return rawSet(name, FuncCall::create(func)); }

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 overload开发者_如何转开发ed function

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


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


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


OK, I did it myself.

typedef const char* (pugi::xml_node::*ChildValueFunctionType)(void) const; // const required!
ChildValueFunctionType ChildValuePointer = &pugi::xml_node::child_value;

Then, just calls

.set("child_value", ChildValuePointer)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号