fork download
  1. #include <functional>
  2. #include <iostream>
  3. #include <type_traits>
  4. #include <utility>
  5.  
  6. struct Vector3f64 {
  7. double x;
  8. double y;
  9. double z;
  10. };
  11.  
  12. struct Vector3f32 {
  13. float x;
  14. float y;
  15. float z;
  16. };
  17.  
  18. // I use this to select their element type in functions:
  19. template <typename T>
  20. using param_vector = std::conditional_t<std::is_same<std::remove_const_t<std::remove_reference_t<T>>, Vector3f64>::value, double, float>;
  21.  
  22. // This is the function I want to pull the return type from:
  23. template <typename T>
  24. T VectorVolume(const T x, const T y, const T z) {
  25. return x * x + y * y + z * z;
  26. }
  27.  
  28. template <typename R, typename... ARGS>
  29. R make_func(R(*ptr)(ARGS...));
  30.  
  31. template <typename T>
  32. decltype(make_func(&VectorVolume<param_vector<T>>)) func(const T& dir) {
  33. return VectorVolume(dir.x, dir.y, dir.z);
  34. }
  35.  
  36. int main() {
  37. const Vector3f64 foo{ 10.0, 10.0, 10.0 };
  38.  
  39. std::cout << func(foo) << std::endl;
  40. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
300