Search code examples
c++templatesc++11specializationvariadic

Specializing return type for a variadic template


First, I'm sorry if I make big english mistakes, I'm french but I'm trying to write as best as I can! Well, I'm struggling with C++11 variadic template. I want to do something a little tricky.

Indeed, I want to specialize the return type of my template, knowing that it is a variadic template.

My goal is to achieve something like:

l.callFun<int>("addition", 40, 1, 1);

The specialization corresponds to the return type that the user want. It is a Lua binding, so I can't determine the return type if the user doesn't precise it (obviously, the default, if no specialization, would be void return). Later, is the name of the function that is called in Lua. Then, the 3 integers corresponds to my variadic template.

Right now, my template is looking like this :

template <typename Z, typename T, typename... U>
Z LuaScript::callFun(const std::string& name, const T& head, const U&... tail);

But it appears that I cannot make a partial specialization of a template function. Is there anybody that could help me ?

Thank you very much!


Solution

  • Help and documentation very much appreciated pheedbaq, but finally, I came with a really simple solution. I didn't tought of it that way, so I'll experiment with this way of operator overloading thanks to you ;)

    What I've done is packing the variadic arguments, and calling another template to specialize on the return type. So I have something like that:

    template <typename Z, typename... T>
    Z LuaScript::callFun(const std::string& name, const T&... args)
    {
        Z ret;
        callFunReal(args);
        [...]
        ret = returnType<Z>();
        return (ret);
    }
    

    It was really quite simple, but couldn't see exactly how to do it... Thanks to everyone! :)