fork download
  1. #include <iostream>
  2.  
  3. void* my_crazy_function(int id) {
  4. static int a = 5;
  5. static double b = 7;
  6. if(id == 1) return &a;
  7. if(id == 2) return &b;
  8. return 0;
  9. }
  10.  
  11. template<typename T, int def>
  12. T vp_to_val(void *p) {
  13. return p ? *static_cast<T*>(p) : static_cast<T>(def);
  14. }
  15.  
  16. int main()
  17. {
  18. std::cout << vp_to_val<int, 0>(my_crazy_function(1)) << std::endl;
  19. std::cout << vp_to_val<int, 0>(my_crazy_function(4)) << std::endl;
  20. std::cout << vp_to_val<double, 1>(my_crazy_function(2)) << std::endl;
  21. std::cout << vp_to_val<double, 1>(my_crazy_function(4)) << std::endl;
  22. }
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
5
0
7
1