Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meta-iteration over variadic templates arguments

I would like to generalize the following pattern:

template<class A1, class A2, class A3>
class Foo {
protected:
  template<class T>
  void foo(const T& t) {...do stuff...}
public:
  void bar(const A1& a) { foo(a); }
  void bar(const A2& a) { foo(a); }
  void bar(const A3& a) { foo(a); }
};

The above approach does not scale with a number of increasing arguments. So, I'd like to do:

template<class As...>
class Foo {
protected:
  template<class T>
  void foo(const t& a) {...do stuff...}
public:
  for each type A in As declare:
  void bar(const A& a) { foo(a); }
};

Is there a way to do it?

like image 460
user3612643 Avatar asked May 23 '16 11:05

user3612643


3 Answers

another approach could be to have a check in bar to test if the type is in the sequence, else barf with a useful error message, this avoid any inheritance tricks..

#include <iostream>

struct E {};
struct F {};

template <class... As>
class Foo
{
    template <typename U>
    static constexpr bool contains() {
        return false;
    }

    template <typename U, typename B, typename ...S>
    static constexpr bool contains() {
        return (std::is_same<U, B>::value)? true : contains<U, S...>();
    }

protected:
    template <class T>
    void foo(const T& a) { std::cout << __PRETTY_FUNCTION__ << std::endl; }

public:
    template <class T>
    void bar(const T& a) {
        static_assert(contains<T, As...>(), "Type does not exist");
        foo(a);
    }
};

int main()
{
    Foo<E, F, E, F> f;
    f.bar(F{});
    f.bar(E{});
    f.bar(1); // will hit static_assert
}
like image 106
Nim Avatar answered Sep 28 '22 09:09

Nim


In case you don't actually need the bars and instead just need to constrain foo - we can use SFINAE to allow a call to it only with a type convertible to one of the As:

template <class... As>
class Foo {
public:
    template <class T,
        class = std::enable_if_t<any<std::is_convertible<T, As>::value...>::value>>
    void foo(T const&) { ... }
};

Where we can implement any with something like the bool_pack trick:

template <bool... b> struct bool_pack  { };
template <bool... b>
using any = std::integral_constant<bool,
    !std::is_same<bool_pack<b..., false>, bool_pack<false, b...>>::value>;
like image 20
Barry Avatar answered Sep 28 '22 07:09

Barry


template <class CRTP, class A, class... As>
struct FooBar
{
    void bar(const A& a)
    {
        static_cast<CRTP*>(this)->foo(a);
    }
};

template <class CRTP, class A, class B, class... As>
struct FooBar<CRTP, A, B, As...> : FooBar<CRTP, B, As...>
{
    using FooBar<CRTP, B, As...>::bar;

    void bar(const A& a)
    {
        static_cast<CRTP*>(this)->foo(a);
    }
};

template <class... As>
class Foo : FooBar<Foo<As...>, As...>
{
    template <class, class, class...>
    friend struct FooBar;

protected:
    template <class T>
    void foo(const T& a) { }

public:
    using FooBar<Foo, As...>::bar;
};

DEMO

like image 27
Piotr Skotnicki Avatar answered Sep 28 '22 09:09

Piotr Skotnicki