What will be displayed when the following code is executed:
#include <iostream>

template<class T>struct Counter {
  Counter() {  ++count;    }
  static int count;
};

template<class T> int Counter<T>::count = 0;

struct Derived1: Counter<Derived1>  {};

struct Derived2: Counter<Derived2>  {};

int main(int argc, char** argv)
{
  Derived1 a, b;
  Derived2 c;
  std::cout << Derived1::count << Derived2::count << '\n';
  return 0;
}
Explanation
The so-called Curiously recurring template pattern (CRTP) takes place​ when the descendent passes himself to the base class as a template parameter. Since Counter<Derived1> and Counter<Derived2> - are different types of objects. Therefore, the correct answer is 21.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes