Literal with number and unit in an algebraic expression as argument

  • A+
Category:Languages

How to design a type that can take an expression in the form of an algebraic expression with numbers and units, where Speed is the type, 23m / 10s could be an expression?

Example:

change_speed(Speed s);    // better: the meaning of s is specified // ... change_speed(2.3);        // error: no unit change_speed(23m / 10s);  // meters per second 

EDIT This question raised the question if a literal in the form of 23m is possible at all.

EDIT the reference to the source has been removed from question for clarity. It can be found here

 


What you have to do is design types that when the expression is evaluated it yields the desired type. With

change_speed(23m / 10s); 

We need 23m and 10s to each yield a type that when you divide them together gives you a Speed. If we have a

struct Meter {     Meter(double value) : value(value) {}     double value; };  struct Second {     Second(double value) : value(value) {}     double value; }  Speeds operator /(const Meter& m, const Second& s) {     return Speed(m.value / s.value); } 

Then we just need to define literal operators for Meter and Second like

Meter operator "" _m(double value) { return Meter(value); }  Second operator "" _s(double value) { return Second(value); } 

we have to use _m and _s to make these valid user defined literals. All literal not from the standard have to start with a _.

Then

change_speed(23_m / 10_s) 

becomes

change_speed(Meter(23) / Second(10)) 

which becomes

change_speed(Speed(2.3)) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: