Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to expand a parameter pack into different member functions?

Tags:

c++

templates

Basically what I'm thinking about is:

template<typename... Types>
class Foo
{
    void Bar(Types var) {};...
};

which when specialized like this:

Foo<S1, S2, S3> foo;

to expand to:

class Foo
{
    void Bar(S1 var){};
    void Bar(S2 var){};
    void Bar(S3 var){};
};

Obviously, it does not make sense in some way but I'll be happy to see some thoughts.

like image 326
Dimitar Mirchev Avatar asked Dec 12 '25 08:12

Dimitar Mirchev


1 Answers

template<class D, class T>
struct FooHelper {
  void Bar(T var) {
    auto* self=static_cast<D*>(this);
    // here use self instead of this
  };
};
template<typename... Types>
class Foo: FooHelper<Foo,Types>...
{
  template<class D,class T>
  friend struct FooHelper<D,T>;
};

this is how you do what you want to do.

We use use the CRTP to give the base class access to everything in Foo.

like image 149
Yakk - Adam Nevraumont Avatar answered Dec 14 '25 00:12

Yakk - Adam Nevraumont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!