fork download
  1. #include <type_traits>
  2.  
  3. /** This is a base class that implements the watching behvaior */
  4. class Watchable
  5. {
  6. // Some stuff...
  7. };
  8.  
  9. /** This is a pointer that let's you observe the lifetime of types extending "Watchable" */
  10. template <typename T>
  11. struct WatchPtr final
  12. {
  13. // Some more stuff...
  14.  
  15. // Make sure that "T" extends "Watchable"
  16. static_assert(std::is_base_of<Watchable, T>::value, "...");
  17.  
  18. private:
  19.  
  20. T* value;
  21. };
  22.  
  23. /** For various reasons, we have chosen to forward-declare this type */
  24. class SomeTypeThatMustBeForwardDeclared;
  25.  
  26. /** This type wants to have a WatchPtr to "SomeType..." */
  27. class SomeOtherType
  28. {
  29. WatchPtr<SomeTypeThatMustBeForwardDeclared> value;
  30. // Here you get some crazy template errors
  31. // "But why? "SomeType..." is defined right below this!
  32. // You don't even really need the definition at this moment anyway!
  33. // Because fuck you, that's why.
  34. };
  35.  
  36. /** Now we define it */
  37. class SomeTypeThatMustBeForwardDeclared : public Watchable
  38. {
  39. // Blah blah blah
  40. };
  41.  
  42. int main() {
  43. // your code goes here
  44. return 0;
  45. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from prog.cpp:1:0:
/usr/include/c++/4.9/type_traits: In instantiation of 'struct std::is_base_of<Watchable, SomeTypeThatMustBeForwardDeclared>':
prog.cpp:12:1:   required from 'struct WatchPtr<SomeTypeThatMustBeForwardDeclared>'
prog.cpp:29:46:   required from here
/usr/include/c++/4.9/type_traits:1384:12: error: invalid use of incomplete type 'class SomeTypeThatMustBeForwardDeclared'
     struct is_base_of
            ^
prog.cpp:24:7: error: forward declaration of 'class SomeTypeThatMustBeForwardDeclared'
 class SomeTypeThatMustBeForwardDeclared;
       ^
prog.cpp: In instantiation of 'struct WatchPtr<SomeTypeThatMustBeForwardDeclared>':
prog.cpp:29:46:   required from here
prog.cpp:12:1: error: 'value' is not a member of 'std::is_base_of<Watchable, SomeTypeThatMustBeForwardDeclared>'
 {
 ^
stdout
Standard output is empty