I have a function like the following, which uses one object A to construct another object B, forwarding the B onto another method.
Code:
static bool ExecuteScriptFunction(CScriptHost* pHost, const std::wstring& strFunctionName, MList* plResult, const MList* plArgs)
{
auto MakeV8 = [&](const MList* pList) -> Local<Value>
{
Local<Value> var;
CreateJSObjectFromCPPObject(pHost->GetIsolate(), pList, var);
return var;
}
Local<Value> value = pHost->InvokeFunction(strFunctionName.c_str(), MakeV8(plArgs));
// Snip //
}
I'd like to overload the function, so that I can pass it a variable amount of plArgs
to pass to InvokeFunction - with the caveat that I need to do it via MakeV8.
To achieve this, std::forward seems like a likely tool for the job, as it means not relying on the less-modern va_* macros, and generally cleaner code, and also learning a bit about a part of the STL I haven't really needed to use before.
I think such an overload would look like the following, however, I am not sure of the behaviour of what I have written, or whether it achieves what I think it does, or whether it achieves anything at all!?
// call a javascript function in V8
template <class... Lists /* = MList*/>
static bool ExecuteScriptFunction(CScriptHost* pHost, const std::wstring& strFunctionName, MList* plResult, const Lists* ...args)
{
auto MakeV8 = [&](const MList* pList) -> Local<Value>
{
Local<Value> var;
CreateJSObjectFromCPPObject(pHost->GetIsolate(), pList, var);
return var;
};
// InvokeFunction handily already takes a variable list of Local<Value>s
// I want to pass a ... of Local<Value>s that is generated from ...args || || || || ||
// So far, no squiggly red lines in VS 2015 on this line of code \/ \/ \/ \/ \/
Local<Value> value = pHost->InvokeFunction(strFunctionName.c_str(), std::forward<Local<Value>>(MakeV8(args))...);
// Snip //
}
Does the following line of code achieve what I'm asking for?
Local<Value> value = pHost->InvokeFunction(strFunctionName.c_str(), std::forward<Local<Value>>(MakeV8(args))...);
Or is there another incantation that will produce the behaviour I require?
Have you tried the following?
The use of
std::forward
is of use only if your arguments are of typeT&&
, which is not the case with your pointers. Search for 'Perfect Forwarding' in case you need more details on that.In order to expand the variadic argument pack,
std::forward
is not nequired, since the function argument list is already a context in which parameter packs are expanded.