fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class T>
  5. class Foo
  6. {
  7. protected:
  8. static T* t;
  9. template <class Self, class U> //T = Type derived from Foo
  10. friend struct Bar;
  11. };
  12. template <class T>
  13. T* Foo<T>::t = nullptr;
  14.  
  15. template <class Self, class T> //T = Type derived from Foo
  16. struct Bar
  17. {
  18. void test()
  19. {
  20. T::t = static_cast<Self*>(this);
  21. }
  22. };
  23.  
  24. struct FooDerived;
  25.  
  26. struct BarDerived : public Bar<BarDerived, FooDerived>
  27. {
  28.  
  29. };
  30.  
  31. struct FooDerived : public Foo<BarDerived>
  32. {
  33.  
  34. };
  35.  
  36. int main() {
  37.  
  38. auto a = BarDerived{};
  39. a.test();
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty