在我的應用程序中,我希望通過傳統函數簽名傳遞參數包,並更改值。這裏是代碼,說明我的問題,我的嘗試作爲評論:使用forward_as_tuple在傳統函數簽名上傳遞參數包
#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 *'作爲回調函數的第一個或最後一個參數傳遞? – Yakk
使用'void *'僅僅是爲了討論,我試圖將實際簽名表示爲中性類型。事實上,實際的簽名參數是'long'這個古老的'LPARAM'。正如MS所述,「WPARAM」和「LPARAM」都是「用於傳遞和返回多態值的類型」。因此,我的問題中沒有傳遞或調用回調。我的興趣直接在於如何更好地理解如何在這樣的傳統構造中轉發任意數量的參數,其中這些參數是智能指針中的值更改引用。 – rtischer8277
僅供參考,我解決了使用'future'的遺留簽名的回調問題請參閱StackOverflow問題[我如何使用PostThreadMessage使用shared_ptr?](http://stackoverflow.com/questions/25667226)並查找以* @ Remy Lebeau和@ rtischer8277到目前爲止已經提交了兩份對我原來的帖子的回覆...... *。 – rtischer8277