Template pack expansion with functions

fonZ

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>();
Barry

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>(), ...);
    }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pack expansion for alias template

Variadic template pack expansion

Variadic template indexed pack expansion

a variadic template as a template parameter without pack expansion

C++ template variable and parameter pack expansion

Variadic template pack expansion argument id

non type template parameter pack expansion

How to "duplicate" template parameter pack expansion?

Variadic template parameter pack expansion looses qualifier

Class template parameter pack expansion for constructors

gcc doesn't accept pack expansion in default template argument

Clang fails to compile parameter pack expansion using template metaprogramming

Simultaneous parameter pack expansion error for unused template type definition

Template pack expansion to apply a function to consecutive pairs of parameters

template Parameter pack expansion on function call with array of data

Single template parameter pack for multiple variadic functions?

Functor variadic template pack expansion for empty pack gives different results in clang++ and g++

when template parameter of a template template-parameter is pack expansion, gcc fails, clang succeeds

How to explicitly tell compiler to choose exactly one parameter template during template pack expansion?

Parameter pack expansion questions

Order of parameter pack expansion

Correct variadic pack expansion

Nested parameter pack expansion

g++ and clang++ are both bugged with template function parameter pack expansion?

Variadic template variable parameter pack expansion in recursive variable definition has incorrect size

Having trouble with parameter pack expansion

Mixin constructors nested pack expansion

parameter pack expansion not working in lambda

Lambda inheritance using pack expansion