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