c++ - Implementing km/h and m/s with user-defined literals -


I want to implement two, _kmh kilometers per hour, and _ms for meters per second. I already have work for two structures and operators:

  constexpr kmoh operator "_ms" (long double val) {return khm {static_cast & lt; Double> gt; (Val * 3.6)}; } Constexpr MS Operator "" _kmh (long double val) {return MS {static_cast & lt; Double & gt; (Val / 3.6)}; }   

I want to implement it in a way that I can do something like this:

  void func (ms speed) {} Int main () {throw (10.0_kim); // It works fun (10.0_ms); // it is not}   

I can not get the work to make another call, because I can not implement it like this:

  constexpr KMH operator "" _kmh (long double val) {return KM {static_cast & lt; Double & gt; (Val)};   

This clearly gives me an error

Error: Functions that differ only in their return types, they can not be overloaded

Is there a way to implement what I want?

You can specify without explicit operator MS ()

  MH of class {public: operator MS () console {returns} {val / 3.6}; } // your past luggage};   

or not clear Creator in MS which km

  class MS {public: CONSTEXPRP MS (CON's MH & KNESH): Well (kmh.val * 3.6) {} // your previous thing};   

BTW, you can live with only one structure ( MS ) and all your operators "_ms , " "_kmh returns MS

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -