lundi 5 octobre 2015

C++ Variadic class template design

I'm working on a mix-in type Config Reader class which supports reading configuration data from environment, command line, files, etc..

I was kind of following the std::tuple type design:

template <class... Ts> struct ConfigReader {};

template <class T, class... Ts>
class ConfigReader<T, Ts...> : ConfigReader<Ts...>
{
  public:
    typedef boost::fusion::set<T, Ts...> sequence_type;

    ConfigReader(T t, Ts... ts)
      : ConfigReader<Ts...>(ts...)
      , parameters_(t, ts...)
    {
      this->init();
    }

  private:
    sequence_type parameters_;
};

But I realized that I could also define this without the recursive inheritance

template <class T, class... Ts>
class ConfigReader<T, Ts...>
{
  public:
    typedef boost::fusion::set<T, Ts...> sequence_type;

    ConfigReader(T t, Ts... ts)
      : parameters_(t, ts...)
    {
      this->init();
    }

  private:
    sequence_type parameters_;
};

The latter case seems to work better because init() is only called once which is really what I want. But now I'm confused as to what are the differences between the two? Am I losing something without the recursive inheritance?

Aucun commentaire:

Enregistrer un commentaire