How can I expand call to variadic template base classes?
Asked Answered
F

1

15

I have a set of non-orthogonal policies, all of them implementing a common named method, the policies add safety checks. I want users to be able to combine the policies to allow more complex validation without creating policies for each combination case by hand. My approach is creating a new policy class to combine others.

The simplified example below shows C as the combining class, here the method id is combined. The expected result is, when calling id on C, to sequentially call the id of each base class.

#include <iostream>
using namespace std;

struct A 
{
    void id() { cout << "A ";}
};

struct B 
{
    void id() { cout << "B ";}
};

template<class A, class... As>
struct C : public A, public As... 
{
    void id()
    {
         A::id();
         As...::id(); // This line does not work, it is illustrative.
    }
};

int main()
{
    C<A, B> c;
    c.id();

    //expected: result A B 
}

The question is: Is it possible to expand As... somehow to do this without using a recursive approach, just using the ... operator?

Feuillant answered 31/5, 2015 at 22:10 Comment(0)
R
32

Sure. You need a context that permits pack expansion - a simple one is a braced initializer list, which also has the benefit of guaranteeing left-to-right evaluation:

using expander = int[];
(void) expander { 0, ((void) As::id(), 0)... };
  • ... expands a pattern to its left; in this case the pattern is the expression ((void) As::id(), 0).

  • The , in the expression is the comma operator, which evaluates the first operand, discards the result, then evaluates the second operand, and returns the result.

  • The (void) cast on As::id() exists to guard against overloaded operator,, and can be omitted if you are sure that none of the As::id() calls will return something that overloads the comma operator.
  • 0 on the right hand side of the comma operator is because expander is an array of ints, so the whole expression (which is used to initialize an element of the array) must evaluate to an int.
  • The first 0 ensures that we don't attempt to create an illegal 0-sized array when As is an empty pack.

Demo.


In C++17 (if we are lucky), the entire body of C::id can be replaced with a binary fold expression: (A::id(), ... , (void) As::id()); Demo.

Reunion answered 31/5, 2015 at 22:14 Comment(2)
Wow, never heard about expanding like this before. It works great and pretty clean to implement with no explicit recursion. :)Feuillant
This is a gem indeed, wish I can up-vote more than once.Parmentier

© 2022 - 2024 — McMap. All rights reserved.