fork download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5. template<typename T>
  6. struct Creator
  7. {
  8. static T* create() { return new T;}
  9. };
  10.  
  11.  
  12. struct Foo : public Creator<Foo>
  13. {};
  14.  
  15. int main() {
  16.  
  17. Foo * foo = new Foo;
  18. cout << "Is Foo? " << (dynamic_cast<Foo*>(foo) != nullptr) << endl;
  19. cout << "Is Creator<Foo>? " << (dynamic_cast<Creator<Foo>*>(foo) != nullptr) << endl;
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Is Foo? 1
Is Creator<Foo>? 1