【发布时间】:2017-04-29 03:10:35
【问题描述】:
在我的应用程序中,我想通过旧函数签名传递参数包,并更改值。以下代码通过我作为 cmets 的尝试来说明我的问题:
#include <tuple>
#include <cassert>
void LegacySignature( void* param );
template< typename... ArgsT >
// using ???; // attempt: can 'template alias' or 'using declaration' make the pack's type visible so I can use it inside the LegacyFunction?
void MyFunc( ArgsT&... args )
{
auto userArgsTuple = std::forward_as_tuple< ArgsT&... >( args... );
LegacySignature( &userArgsTuple );
}
void LegacySignature( void* param )
{
// auto userArgsTuple = reinterpret_cast<???>( param ); // attempt: how can I get the parameter pack's type declared so I can use it here?
// do something with the params like change num to 44 and tf to true;
//userArgsTuple->num = 44; // desired functionality
//userArgsTuple->tf = true; // desired functionality
}
int main()
{
int num { 33 };
bool tf { false };
MyFunc( num, tf );
assert( num == 44 && tf == true );
return 0;
}
有没有办法让参数包成为可声明的左值?
【问题讨论】:
-
作为猜测,他们希望您传递回调。回调采用
void*和它们提供的其他一些参数。您想通过void*传递一些数据,以便在另一端传回给您。他们不会将您的回调或void*存储的时间长于您可以控制的固定范围。它是否正确?问题:void*是作为第一个或最后一个参数传递给回调函数的吗? -
使用
void*只是为了讨论,我试图将实际签名表示为中性类型。事实上,实际的签名参数是古老的LPARAM,即long。正如 MS 所说,WPARAM和LPARAM都是“用于传递和返回多态值的类型”。因此,在我的问题中没有传递或调用回调。我的兴趣直接在于更好地理解如何在这种遗留结构中转发任意数量的参数,其中这些参数是智能指针中的值可变引用。 -
仅供参考,我使用
future解决了对遗留签名的回调问题,请参阅 StackOverflow 问题 [如何使用 PostThreadMessage 使用 shared_ptr?](stackoverflow.com/questions/25667226) 并查找以 @Remy Lebeau 和 @rtischer8277 到目前为止已经为我的原始帖子提交了两个答案…….
标签: c++11 parameters parameter-passing using-declaration template-aliases