I control the conversion of units. Tell us that I have reached the state in which I am achieving this.
The heart of my conversion between different units rests on the following common template function:
template <class SrcUnit, class TgtUnit> extern double convert(double val);
The purpose of this function is to convert a physical quantity, expressed in units of type SrcUnit , into another, expressed in units of type TgtUnit .
I have a class called Quantity<Unit> that manages values โโand their unities, and this class is trying to provide type safety and automatic conversion. For example, I export the following constructor:
template <class SrcUnit> Quantity(const Quantity<SrcUnit> & q) : unit(UnitName::get_instance()) { check_physical_units(q);
I export other material where the conversion is done.
So, when someone wants to manage a new module, she points to the new Unit class. I achieve this by inserting into the macro all the necessary specification of the new class. Now the user must write the conversion functions. That is, write two specializations of the convert() template. For example, suppose you have a unit called "Kilometer and you wish to specify a new unit called Mile`. In this case, you do the following:
Declare_Unit(Mile, "mi", "English unit of length", Distance, 0, numeric_limits<double>::max()); // macro instantiating a new Unit class template <> double convert<Kilometer, Mile>(double val) { return val/1609.344; } template <> double convert<Mile, Kilometer>(double val) { return 1609.344*val; }
Now, what happens if the user forgets to write a conversion function? Well, in this case, the linker will fail because it cannot find the specialization of convert() .
Now my question.
Although I think the linker error is acceptable, since the behavior for the message to the user is missing convert() , I would like to check the compilation time I have for the existence of the convert() specialization. So my question is: how can I achieve this? I think through static_assert put before each call to convert() , which checks if the specialization is known. But how to do that?
PS: It would also be very useful for me if someone could recommend me a good text about C ++ metaprogramming.