I am writing a template class which takes a floating-point-like type (float, double, decimal, GMP) as a parameter. However, my class requires various numeric constants. Some of these are rational numbers (int/int) while others are irrational and available to 30 or so decimal places.
What is the best way to go about initialising these constants, so:
T c1 = <constant>;
where T is the templated type?
While I could always fall-back on doubles (T c1 = 0.1415926535…) and rely on the compiler/implicit initialiser to convert to the appropriate type I would like to retain the extra precision if at all possible.
I am interested in both current solutions and those which C++0x (or is it C++1x?) might bring to the table.
I think the easiest way to do this is to create a specialized container class that holds the constants, something like this:
In your real class you can then do something like this:
This avoid that you have to write complete specialization classes only to redefine those constants.
Note that default behavior can fall back to implicit double assignment.