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. {
  26. return x * x + y * y + z * z;
  27. }
  28.  
  29. template<class R, class... ARGS>
  30. std::function<R(ARGS...)> make_func(R(*ptr)(ARGS...)) {
  31. return std::function<R(ARGS...)>(ptr);
  32. }
  33.  
  34. // This function fails to compile:
  35. template <typename T>
  36. typename decltype(make_func(&VectorVolume<param_vector<T>>))::result_type func(const T& dir)
  37. {
  38. return VectorVolume(dir.x, dir.y, dir.z);
  39. }
  40.  
  41. int main() {
  42. const Vector3f64 foo{ 10.0, 10.0, 10.0 };
  43.  
  44. std::cout << func(foo) << std::endl;
  45. }
  46.  
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
300