-1

I came across the below piece of code in a header file.

template <int x, template <typename T> class Car,
          typename DOC, typename COMMON,
          typename filter = imp::Filter>
class CurrentClass
    : public imp::base<
          x, Car, DOC,
          CurrentClass<x, Car, DOC, COMMON>, Filter> {
public:
  COMMON *common;
  CurrentClass(Connection &cnt, COMMON *common_)
      : imp::base<
            x, Car, DOC,
            CurrentClass<x, Car, DOC, COMMON>,
            Filter>(cnt),
       common(common_) {}
};

Can someone please explain the different parts of this code in terms of templates and class inheritance?

According to my understanding CurrentClass inherits from the base class declared in imp class.

Thanks

Learner
  • 85
  • 2
  • 10
  • 4
    Please don't ever post images of code. Post code as *text*. – Jesper Juhl Jul 18 '20 at 13:08
  • 4
    *Can someone please explain the different parts of this code in terms of templates and class inheritance* -- That is a job for a good C++ book. – PaulMcKenzie Jul 18 '20 at 13:13
  • 2
    This is an example of a fairly advanced use of C++ templates. To fully explain what's happening here requires, probably, six months to a year of learning how templates work, beforehand, before starting to understand how templates are used here. Obviously, explaining a year's worth of learning C++ templates is not something that can be done in a paragraph or two on stackoverflow.com. The best way for you to learn this is to continue following the methodical, guided approach that your C++ textbook uses to present this material, until you understand this. – Sam Varshavchik Jul 18 '20 at 13:40
  • @SamVarshavchik can you please suggest a good c++ book. – Learner Sep 06 '20 at 12:07
  • 1
    See stackoverflow.com's [list of C++ textbooks](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Sep 06 '20 at 12:47

1 Answers1

1

There is a lot of noise in this code and as mentioned, it is quite advanced, but google the points I note below. There are several distinct things I can point out:

1.

template <int x, template <typename T> class Car,
      typename DOC, typename COMMON,
      typename filter = imp::Filter>

The "int x" part require that argument to be an integer

The "template <typename T> class Car" is known as a template-template. It's essentially pulling the template-type out of a type. Example: std::vector<int> - the template-template is "int".

Typename filter = imp::filter Is just a default template argument and is optional.

2. You will notice that the class CurrentClass inherits from some base class imp::Base with some template arguments: x, car, doc. You will then notice it passes itself + it's template arguments *to the base class. This is known as CRTP, and it's a way to create c++ virtuals, using templates! Very powerful stuff.

class CurrentClass
: public imp::base<
      x, Car, DOC,
      CurrentClass<x, Car, DOC, COMMON>, Filter> {

I've highlighted (with square brackets) the important bits of the CRTP below:

class CurrentClass
[: public imp::base<]
      x, Car, DOC,
      [CurrentClass<x, Car, DOC, COMMON>, Filter>] {

The constructor is just initialising the base class. Which is normal code.

g-radam
  • 527
  • 1
  • 7
  • 20