fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef void (*F)();
  5.  
  6. template<F f> struct Goal
  7. {
  8. Goal() { f(); }
  9. };
  10.  
  11. template<F f> struct Singleton
  12. {
  13. static Goal<f> instance;
  14. };
  15. template<F f> Goal<f> Singleton<f>::instance;
  16.  
  17. template<F f, Goal<f>* I> struct Instantiator {};
  18.  
  19. template<F f> struct Voila : Instantiator<f, &Singleton<f>::instance> {};
  20.  
  21. void foo() { cout << "foo" << endl; }
  22. void bar() { cout << "bar" << endl; }
  23. Voila<foo> voici;
  24.  
  25. int main() {
  26. Singleton<bar> tmp;
  27. return 0;
  28. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
foo