将重载函数作为模板传递

发布于 2024-11-01 17:29:26 字数 590 浏览 1 评论 0原文

我有这样的模板集

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

,我使用它:

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

但是 child_value 超载

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

并且编译器给出了这个错误:

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

我怎样才能修复这个错误?我想要 child_value() 版本。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷不放纵 2024-11-08 17:29:26

将 typedef 定义为:

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

然后编写:

//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

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
深海少女心 2024-11-08 17:29:26

我认为需要显式转换:
.set( "child_value", static_cast( &pugi::xml_node::child_value ) );

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

疑心病 2024-11-08 17:29:26

好的,
我自己做的。

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

然后,只需调用

.set("child_value", ChildValuePointer)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文