fork download
  1. #include <type_traits>
  2.  
  3. namespace foo {
  4. struct Vector {
  5. double x, y, z;
  6.  
  7. Vector(double x, double y, double z) :
  8. x{x}, y{y}, z{z}
  9. {}
  10.  
  11. template<class T>
  12. Vector(const T& foreign) :
  13. x{foreign.x}, y{foreign.y}, z{foreign.z}
  14. {}
  15.  
  16.  
  17. template<class T
  18. ,
  19. class = typename std::enable_if<
  20. std::is_constructible<T, double,double,double>::value
  21. >::type
  22. >
  23. operator T() const
  24. {
  25. return T{x, y, z};
  26. }
  27. };
  28.  
  29.  
  30. Vector makeAVector()
  31. {
  32. return {1, 2, 3};
  33. }
  34. }
  35.  
  36.  
  37. namespace bar {
  38.  
  39. struct Vector3 {
  40. using Scalar = double;
  41.  
  42. Scalar x, y, z;
  43.  
  44. Vector3(Scalar x, Scalar y, Scalar z) :
  45. x{x}, y{y}, z{z}
  46. {}
  47.  
  48. Vector3(const Vector3& other) :
  49. x{other.x}, y{other.y}, z{other.z}
  50. {}
  51.  
  52. Vector3(const Scalar *ptr) :
  53. x{ptr[0]}, y{ptr[1]}, z{ptr[2]}
  54. {}
  55.  
  56. };
  57.  
  58. void doSomething(const Vector3& v, double* x)
  59. {}
  60.  
  61. void doSomething(const Vector3& v, const Vector3& x)
  62. {}
  63.  
  64. }
  65.  
  66.  
  67. int main()
  68. {
  69. bar::Vector3 a{2, 3, 4};
  70.  
  71. bar::doSomething( a, foo::makeAVector() );
  72. }
  73.  
Success #stdin #stdout 0s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty