Simple mathematical operations (add, sub, mul, div) in C++11 template I made a simple script to implement basic mathematics operations by using variadic functions. I would like to know if my implementation is correct.

The code only works for Visual C++ compiler Nov 2013 CTP (CTP_Nov2013).

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;exception&gt;
using namespace std;

template &lt;typename T&gt;
T add(T&amp;&amp; item = T())
{
    return forward&lt;T&gt;(item);
}

template &lt;typename T, typename ... Types&gt;
auto add(T&amp;&amp; first, Types&amp;&amp; ... rest)
    -&gt; decltype(first + add(forward&lt;Types&gt;(rest)...))
{
    return forward&lt;T&gt;(first) + add(forward&lt;Types&gt;(rest)...);
}

template &lt;typename T&gt;
T sub(T&amp;&amp; i = T())
{
    return forward&lt;T&gt;(i);
}

template &lt;typename T, typename ... Types&gt;
auto sub(T&amp;&amp; first, Types&amp;&amp; ... rest)
-&gt; decltype(forward&lt;T&gt;(first) - sub(forward&lt;Types&gt;(rest)...))
{
    return forward&lt;T&gt;(first) - sub(forward&lt;Types&gt;(rest)...);
}

template &lt;typename T&gt;
T multiple(T&amp;&amp; i = T())
{
    return forward&lt;T&gt;(i);
}

template &lt;typename T, typename ... Types&gt;
auto multiple(T&amp;&amp; first, Types&amp;&amp; ... rest)
-&gt; decltype(forward&lt;T&gt;(first) * multiple(std::forward&lt;Types&gt;(rest)...))
{
    return forward&lt;T&gt;(first) * multiple(forward&lt;Types&gt;(rest)...);
}

template &lt;typename T&gt;
T divide(T&amp;&amp; item = T())
{
    return forward&lt;T&gt;(item);
}

template &lt;typename T, typename ... Types&gt;
auto divide(T&amp;&amp; first, Types&amp;&amp; ... rest)
    -&gt; decltype(forward&lt;T&gt;(first) / divide(forward&lt;Types&gt;(rest)...))
{
    if (divide(forward&lt;Types&gt;(rest)...) == 0)
        throw "Opps divided by Zero";
    return forward&lt;T&gt;(first) / divide(forward&lt;Types&gt;(rest)...);
}

template&lt;typename... Types&gt;
void termnate(Types&amp;&amp;...)
{
    std::cout &lt;&lt; '\n';
}

template&lt;typename... Types&gt;
void result(Types&amp;&amp;... t)
{
    termnate{ ([&amp;]{ std::cout &lt;&lt; forward&lt;Types&gt;(t) &lt;&lt; ' '; }(), 1)... };
}

int main()
{
    result(     add(1, 2.5, 3, 4, 5), " = ", "1 + 2,5 + 3 + 4 + 5");

    result(sub(sub(sub(sub(1, 2.5), 3), 4), 5), " = ", "1 - 2.5 - 3 - 4 - 5"); // OK = -13.5

    result(multiple(1, 2.5, 3, 4, 5), " = ", "1 x 2.5 x 3 x 4 x 5");

    result(divide(divide(divide(divide(1, 2.5), 3), 4.5), 5), " = ", "1 / 2.5 / 3 / 4 / 5"); // OK = .005926


    cout &lt;&lt; add("\nTest ", "template: ", string("PASS\n"));
}
When passing around move references, the recommended is to forward the parameters to ensure that the move reference is preserved:

template &lt;typename T, typename ... Types&gt;
auto add(T&amp;&amp; first, Types&amp;&amp; ... rest) 
    -&gt; decltype(first + add(std::forward&lt;Types&gt;(rest)...))
{
    return first + add(std::forward&lt;Types&gt;(rest)...);
}


Note that you have to explicitly provide the template type to std::forward(). It would deduce the wrong type otherwise.