roystgnr / MetaPhysicL

Metaprogramming and operator-overloaded classes for numerical simulations
Other
22 stars 12 forks source link

Permit explicit cast to non-dual number #40

Closed dschwen closed 5 years ago

dschwen commented 5 years ago

To make code that is templated on dual/non-dual more generic it'd be great if an explicit static_cast<double>(dual_number) was permitted. This would help with iostream output and cases where the derivatives can be dropped.

lindsayad commented 5 years ago

It seems like raw_value could do what you want (in raw_type.h. Specializations of RawType in their respective headers (e.g. dualnumber_decl.h for DualNumber))

dschwen commented 5 years ago

MetaPhysicL::raw_value(dual_number) does work. I assume the reason the cast was not implemented is that the explicit keyword was only added in C++11 ?

roystgnr commented 5 years ago

Hmm... that was the original reason, but actually even with the explicit keyword I'm leaning against adding a cast operator.

Right now, if you take a non-generic code and you try to make it generic, but you miss a spot where the original author directly initialized a double from an input, the compiler will scream at you that you missed a spot. Having to use raw_value ranges from annoying (when you have to change your generic code) to extremely annoying (when you can't change a third-party generic code) but the alternative risk, that the code might compile and silently give incorrect derivatives, is horrific.

I could possibly be talked out of this. Or more probably talked into making it a non-default compile-time option, if that would be convenient enough?

dschwen commented 5 years ago

I see your point @roystgnr ! Let's drop this.