fonZ
fonZ

Reputation: 2479

Template pack expansion with functions

The code below works as intended.

struct A 
{
    template<typename T>
    void do_real_stuff() {}

    template <typename... Types> 
    struct DoStuff;

    template <typename Head, typename... Tail>
    struct DoStuff<Head, Tail...>
    {
        DoStuff(A &base)
        {
            base.do_real_stuff<Head>();
            (base.do_real_stuff<Tail>(), ...);
        }
    };
};

struct A allows me to call it like:

A a;
DoStuff<int,double,string>(a);

But I can't figure out why I can't get the same pattern to work with functions instead of structs.

struct B 
{
    template<typename T>
    void do_real_stuff() {}

    template <typename... Types>
    void DoStuff();

    template <typename Head, typename... Tail>
    void DoStuff<Head, Tail...>()
    {
         do_real_stuff<Head>();
         (do_real_stuff<Tail>(), ...);
    }
};

Because I want to call it like this:

A a;
a.DoStuff<int,double,string>();

Upvotes: 0

Views: 40

Answers (1)

Barry
Barry

Reputation: 303890

You can't partially specialize function templates - only class templates.

But you don't actually have to do that in this example. You're already using a fold-expression, just also include the first type:

struct B 
{
    template<typename T>
    void do_real_stuff() {}

    template <typename... Types>
    void DoStuff()
    {
         (do_real_stuff<Types>(), ...);
    }
};

Upvotes: 4

Related Questions