我正在開發一個 C 14 專案,但我不知道如何將帶有引數包的函式傳遞給另一個帶有引數包的函式。
template <typename F, typename... Args, typename Callback, typename ... Args2>
bool doAsyncRpc(F func,
Callback callback,
const char* routeType,
Args&&... args, Args2&&... args2) {
Request req;
SetReqValue(&req, std::forward<Args>(args)...);
func(routeType, req, callback, std::forward<Args2>(args2)...);
return true;
}
如您所見,func在函式中呼叫doAsyncRpc。args用于初始化引數req,callback并args2用于傳遞func.
Callback是一個函式,Args2是它的引數。
有點復雜...
總之,我不知道如何設計函式doAsyncRpc,以便它可以接受兩個帶有引數包的函式。
uj5u.com熱心網友回復:
您不能從呼叫的引數中推斷出兩個包,因此您必須至少將其包裝Args...在一個元組中,但兩者都做可能更容易。
然后,您需要將其他引數與這些引數一起放入元組中。
template <typename F, typename... Args, typename Callback, typename ... Args2>
bool doAsyncRpc(F func,
Callback callback,
const char* routeType,
std::tuple<Args...> args,
std::tuple<Args2...> args2) {
Request req;
std::apply(SetReqValue, std::tuple_cat(std::make_tuple(&req), args));
std::apply(func, std::tuple_cat(std::make_tuple(routeType, req, callback), args2));
return true;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474913.html
上一篇:編譯器宏來比較型別的位元組大小
