C++ variadic template return type

Viewed 6856

My function calls a Python function with a set of given input arguments (variadic) and returns a tuple containing the function's output (variadic because output varies on the function called).

I'm compiling on a Windows 10 machine via MinGW-w64 compiler's port of g++ using C++11. I've declared this templated variadic function (and call it) as follows:

// Declaration (in PyInterface.h)
template <typename... Inputs, typename... Outputs>
const std::tuple<Outputs...>& callFunction(const std::string &modulePath, const std::string &funcName, Inputs&&... args) { 
    // Calls a Python function & packs output variables into a std::tuple... 
}

// Sample call (in main.cpp)
const std::tuple<std::string, std::string> output = pi.callFunction(myModulePath, myFuncName, inputArg1, inputArg2, inputArg3);

However, this error is thrown (shortened with ... for readability):

conversion from 'const std::tuple<>' to non-scalar type 'const std::tuple<std::__cxx11::basic_string<...>, std::__cxx11::basic_string<...> >' requested

As far as I'm aware using two variadic templates is legal. Additionally, it seems to me that the return type is explicitly set by the variable (const std::tuple<std::string, std::string> output) I am attempting to assign the result of the function to, so the compiler should know what the desired return type is.

Originally, I thought that the error simply indicated that I wasn't using the variadic templates appropriately. I tried using nested template parameters (illustrated here) to specify the return type (i.e., const T<Outputs...>& callFunction). However, this only succeeded in producing an error message indicating that template deduction of T had failed.

This error indicates that my intuition is wrong and that, in my original function, the compiler isn't deducing the desired return type from output's type.

Why is my intuition wrong? How do I correctly use a variadic template to specify this function's return type?

2 Answers
Related