【发布时间】:2019-01-23 13:18:54
【问题描述】:
我想要一个函数,它接受指向函数的指针并转发函数指针类型本身给出的所有参数,如下所示:
template < typename RET, typename ... ARGS >
auto Do1( RET(*ptr)(ARGS...), ARGS... args )
{
(*ptr)(std::forward<ARGS>( args )...);
}
int main ()
{
int i=4;
Do1( &Ex1, i );
Do1( &Ex2, i ); //fails!
Do1( &Ex3, i+1 ); // fails
}
两个例子都需要调用的函数:
void Ex1( int i){ std::cout << __PRETTY_FUNCTION__ << " " << i << std::endl; i=10;}
void Ex2( int& i){ std::cout << __PRETTY_FUNCTION__ << " " << i << std::endl; i=20;}
void Ex3( int&& i){ std::cout << __PRETTY_FUNCTION__ << " " << i << std::endl; i=30;}
在Ex2和Ex3的情况下它会失败,因为它尝试推断ARGS列表的类型两次并且结果不同。编译器抱怨:
main.cpp:57:22: error: no matching function for call to 'Do1(void (*)(int&), int&)'
Do1( &Ex2, i ); //fails!
^
main.cpp:33:10: note: candidate: 'template<class RET, class ... ARGS> auto Do1(RET (*)(ARGS ...), ARGS ...)'
auto Do1( RET(*ptr)(ARGS...), ARGS... args )
^~~
main.cpp:33:10: note: template argument deduction/substitution failed:
main.cpp:57:22: note: inconsistent parameter pack deduction with 'int&' and 'int'
之后,我尝试使用以下方法解决该问题,因为我只通过一次推导 ARGS 列表来提取类型,然后再次转发到中间 lambda,如下所示:
template < typename RET, typename ... ARGS >
auto Do2( RET(*ptr)(ARGS...) )
{
return [ptr]( ARGS ... args )
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
(*ptr)(std::forward<ARGS>(args)...);
};
}
int main ()
{
int i=4;
Do1( &Ex1, i );
Do1( &Ex2, i ); //fails!
Do1( &Ex3, i+1 ); // fails
Do2( &Ex1 )( i );
std::cout << "now i: " << i << std::endl;
std::cout << std::endl;
Do2( &Ex2 )( i );
std::cout << "now i: " << i << std::endl;
std::cout << std::endl;
Do2( &Ex3 )( i+1 );
std::cout << "now i: " << i << std::endl;
std::cout << std::endl;
}
问:有什么方法可以修复第一种方法以摆脱这里的中间 lambda?如果没有,中间 lambda 的解决方案是否设计得“好”,尤其是所有“转发”的东西,这样我就不会创建一些副本或其他意外行为?
编辑:
这只是一个简化的例子。我不打算写一份std::invoke。所以在我的真实世界代码中,Do 方法本身还有很多事情要做。
从函数指针类型中获取所需类型很重要,因为我必须在Do 中执行一些检查,这些检查与函数指针提供的类型相关,而 not 来自给定我从用户代码向Do 方法提供的附加参数。
【问题讨论】:
-
这看起来在逻辑上等同于
std::invoke。只需使用std::invoke。 -
@SamVarshavchik:这只是示例代码!在现实世界的代码中,除了通过指针调用给定的函数之外,还需要做更多的事情......
-
您可以通过将函数参数包放在非推导上下文中来禁用推导,让函数指针引导推导。
-
@Jans:你能举个例子吗?
-
@Klaus - Jarod42 已经在他的回答中提供了它。第一部分。
标签: c++ c++14 variadic-templates perfect-forwarding