gpt4 book ai didi

c++ - 将参数转发给模板成员函数

转载 作者:IT老高 更新时间:2023-10-28 23:21:57 26 4
gpt4 key购买 nike

ideone example


我需要将一些预定义的参数和一些用户传递的参数转发给成员函数。

#define FWD(xs) ::std::forward<decltype(xs)>(xs)

template<class T, class... Ts, class... TArgs>
void forwarder(void(T::*fptr)(Ts...), TArgs&&... xs)
{
T instance;
(instance.*fptr)(FWD(xs)..., 0);
// ^
// example predefined argument
}

forwarder(&example::f0, 10, 'a');
forwarder(&example::f1, 10, "hello", 5);

这适用于非模板成员函数。

传递给 forwarder 的成员函数指针也可以指向模板函数。不幸的是,在这种情况下,编译器无法推断出 T 的类型:

struct example
{
void f0(int, int) { }

template<class T>
void f1(T&&, int) { }
};

// Compiles
forwarder(&example::f0, 10);

// Does not compile
forwarder(&example::f1, 10);

错误:

prog.cpp:30:28: error: no matching function for call to 'forwarder(<unresolved overloaded function type>, int)'
forwarder(&example::f1, 10);
^
prog.cpp:20:6: note: candidate: template<class T, class ... Ts, class ... TArgs> void forwarder(void (T::*)(Ts ...), TArgs&& ...)
void forwarder(void(T::*fptr)(Ts...), TArgs&&... xs)
^
prog.cpp:20:6: note: template argument deduction/substitution failed:
prog.cpp:30:28: note: couldn't deduce template parameter 'T'
forwarder(&example::f1, 10);

有什么方法可以帮助编译器推断出正确的类型而不改变forwarder的接口(interface)?

如果不是,在不使用户语法过于复杂的情况下解决此问题的最佳方法是什么?

编辑: 将成员函数指针作为模板参数传递也是可以接受的,可能通过包装器。目标成员函数在编译时总是已知的。伪代码:

forwarder<WRAP<example::f0>>(10, 'a');
// Where WRAP can be a macro or a type alias.

ideone example

最佳答案

我通过为成员函数指针提供模板参数,在 gcc 4.9 中编译了您的代码;像这样

int main(){
// Compiles
forwarder(&example::f0, 10);
//Does not compile
forwarder(&example::f1, 10);
//Does compile, instantiate template with int or what ever you need
forwarder(&example::f1<int>,10)
}

我相信你需要实例化模板成员函数。这会改变你的界面吗?我认为任何答案都将围绕以某种方式实例化该成员模板。

关于c++ - 将参数转发给模板成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32016749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com