Recursive inheritance variation pattern and using declaration

I want to do something similar to the code below, except that I do not want to implement func()twice, because it will be the same implementation. Do you have any suggestion on how to do this?

template <typename First, typename... Rest>
class Base : public Base<Rest...> {
public:
    using Base<Rest...>::func;
    void func(First object) {
        // implementation
    }
};

template <typename First>
class Base<First> {
public:
    void func(First object) {
        // implementation
    }
};

struct X {};
struct Y {};
struct Z {};

class Derived : public Base<X, Y, Z> {};

// ...

Derived d;
d.func(X{});
d.func(Y{});
d.func(Z{});
+4
source share
4 answers

With multiple inheritance and an additional directive using:

template <typename First, typename... Rest>
struct Base : Base<First>,  Base<Rest...> {
    using Base<First>::func;
    using Base<Rest...>::func;
};

template <typename First>
struct Base<First> {
    void func(First object) {/*...*/}
};

Live Demo (C ++ 11)

+3
source

Maybe this is a solution? (Move func definition to a common base?)

template <class First>
class Func_impl
{
public:
     void func(First obj) {
     // implementation
     }
}

template <typename First, typename... Rest>
class Base : public Base<Rest...>, Func_impl<First> {
public:
    // no need to declare func here
};


template <typename First>
class Base<First> : public Func_impl<First> {
public:
   // no need to declare func here
};
+2
source

(1) Base

template <typename...>
class Base;

(2) ,

template <typename First, typename... Rest>
class Base<First, Rest...> : public Base<Rest...> {
public:
    using Base<Rest...>::func;
    void func(First object) {
        // implementation
    }
};

(3) (4)

template <>
class Base<>
 { public: void func () {} };

( ) func() : ( - func) using Base<Rest...>::func;.

.

#include <iostream>

template <typename...>
class Base;

template <typename First, typename... Rest>
class Base<First, Rest...> : public Base<Rest...> {
public:
    using Base<Rest...>::func;
    void func(First object)
     { std::cout << "func() recursive" << std::endl; }
};

template <>
class Base<>
 { public: void func () {} };

struct X {};
struct Y {};
struct Z {};

class Derived : public Base<X, Y, Z> {};

int main()
 {
   Derived d;
   d.func(X{});
   d.func(Y{});
   d.func(Z{});
 }
+2

++ 17 , "" " " ( -). , , , . .

http://coliru.stacked-crooked.com/a/f67d00da68226d5f

#include <iostream>
#include <utility>

template<std::size_t i, class T>
struct IndexedNode {
  void func(T) {
    std::cout << __PRETTY_FUNCTION__ << std::endl;
  }
};

template<class Indices, class... Ts>
struct IndexedBase;

template<std::size_t... is, class... Ts>
struct IndexedBase<
  std::index_sequence<is...>,
  Ts...
>
  : IndexedNode<is, Ts>...
{
  using IndexedNode<is, Ts>::func...;
};

template<class... Ts>
struct Base
  : IndexedBase<std::index_sequence_for<Ts...>, Ts...>
{
  using Is = std::index_sequence_for<Ts...>;
  using IndexedBase<Is, Ts...>::func;
};
+1

Source: https://habr.com/ru/post/1689679/


All Articles