fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4.  
  5. class Base { public: Base() { std::cout << "Base-Ctor" << std::endl; }};
  6. std::vector<Base*> elements;
  7.  
  8. template <typename T>
  9. class A : public Base
  10. {
  11. public:
  12. A()
  13. {
  14. std::cout << "A-Ctor begin" << std::endl;
  15. elements.push_back(&(T::get()));
  16. std::cout << "A-Ctor end" << std::endl;
  17. }
  18. };
  19. class B : public A<B>
  20. {
  21. public:
  22. B() { std::cout << "B-Ctor" << std::endl; }
  23. static B& get()
  24. {
  25. std::cout << "create static" << std::endl;
  26. static B b;
  27. std::cout << "return static" << std::endl;
  28. return b;
  29. }
  30. };
  31.  
  32. int main(void)
  33. {
  34. B b;
  35.  
  36. std::cout << elements.size() << std::endl;
  37. }
Runtime error #stdin #stdout #stderr 0s 3232KB
stdin
Standard input is empty
stdout
Base-Ctor
A-Ctor begin
create static
Base-Ctor
A-Ctor begin
create static
stderr
terminate called after throwing an instance of '__gnu_cxx::recursive_init_error'
  what():  std::exception