I'm in a bit of a dilemma. I want to know what the difference is between Foo and Meh. I wrote both of them and I did not like Foo so I wrote Meh. They both work but is there an overhead to Meh? Is there anything bad about Meh?

The reason I'm asking is because everywhere I look on the internet, I see coders writing Variadic templates the same way Foo is written. With a helper function. I dislike using a helper function for such a thing so I wrote Meh which uses an initializer list and a for-ranged loop to go through the packed parameters.

What's the pros and cons of using Foo vs. Meh? Are there downsides? Does one have an overhead? Which do you prefer?

#include<iostream>

template<typename T>
void Foo(T Argument)
{
    std::cout<<Argument;
}

template<typename First, typename... Args>
void Foo(First ArgumentOne, Args... Remainder)
{
    std::cout<<ArgumentOne;
    Foo(Remainder...);
}



template<typename First, typename... Args>
void Meh(First ArgumentOne, Args... Remainder)
{
    for (First I : {ArgumentOne, Remainder...})
    {
        std::cout<<I;
    }
}



int main()
{
    Foo(1, 2, 3, 4);
    std::cout<<std::endl;
    Meh(1, 2, 3, 4);

    //Both of the above print "1234".
}

Well, there is a huge difference between the two. Try compiling this:

Foo(true, 2, 5.0, "dude");
Meh(true, 2, 5.0, "dude");

Meh requires that all parameters have the same type (or, at least, all be convertible to the type of the first argument). That's not the case for the Foo version, which can allow the parameters to be of completely unrelated types.

Basically, this is like comparing std::tuple and std::array. Of course, if all the types are the same (or it is acceptable to convert them all to one type), then you should use std::array or analogously, Meh from your example. However, if you don't want to impose that restriction, then you'll have to use std::tuple and/or variadic templates (as Foo from your example).

What's the pros and cons of using Foo vs. Meh? Are there downsides?

They solve different problems. Foo is more general than Meh. Using Foo to solve a problem that Meh can solve is overkill. That's about it. Well, except for the fact that the syntax of Foo is really annoying, believe me, I know.

Does one have an overhead?

No. Everything will be inlined and unrolled equally well in both cases, no overhead. For very large sequences, Meh might be more cache-effective.

Which do you prefer?

The one that solves the problem at hand.

commented: :o definitely didn't notice that Meh had restrictions :) Thanks A LOT! +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.