10

I'm trying to find a solution to have constant numeric literals inside template class method. I'm making some math template classes to be used with float or double types. The problem is that literals are different depending on data type (for example "0.5f" for float and "0.5" for double). So far, I come up with two solutions. Some hypothetical code for first one:

template <typename T>
class SomeClass
{
    public:
        T doSomething(T x);
};

template <>
float SomeClass<float>::doSomething(float x)
{
    float y = 0.5f;
    /*
     * Do computations...
    */
    return x;
}

template <>
double SomeClass<double>::doSomething(double x)
{
    double y = 0.5;
    /*
     * Do computations...
    */
    return x;
}

The approach above forces rewriting whole methods for every type it is used with.

Another approach:

template <typename T>
class SomeClass
{
    public:
        T doSomething(T x);

    private:
        T getValue();
};

template <typename T>
T SomeClass<T>::doSomething(T x)
{
    T y = getValue();
    /*
     * Do computations...
    */
    return x;
}

template <>
float SomeClass<float>::getValue()
{
    return 0.5f;
}

template <>
double SomeClass<double>::getValue()
{
    return 0.5;
}

This one doesn't require to write same methods multiple times for specific type, but requires to have a lot getValue() methods for every "magic number" that need to be used inside the method.

Is there another, "more elegant" way to solve this?

2
  • 1
    If its only about literals, I think you can use casting static_cast<T>()
    – Mohammad
    Jun 18, 2012 at 17:39
  • I didn't mention it in my question, but I would like to avoid any conversions, neither explicit nor implicit.
    – umebe
    Jun 18, 2012 at 18:03

3 Answers 3

1

Assuming it's actually necessary to use different values in the two specializations (it's not necessary for 0.5 and 0.5f, for example) it would be a lot less typing to do:

template <typename T>
class SomeClass
{
  public:
    T doSomething(T x);

  private:
    static const T magic_number_1;
};

template <typename T>
T SomeClass<T>::doSomething(T x)
{
  T y = magic_number_1;
  /* 
   * Do computations...
  */
  return x;
}

template <>
const float SomeClass<float>::magic_number_1 = 0.5f;

template <>
const double SomeClass<double>::magic_number_1 = 0.5;
1
  • Right, this seems to be better that separate getValue() methods.
    – umebe
    Jun 18, 2012 at 18:26
1

Thank you all for your answers and comments. I allowed myself to make a summary of what has been said so far and add my conclusions.

I'm going to implement some math class template to be instantiated to use with float or double type. There is a need to use some numeric literals internally in the class. They will be some commonly used numeric literals and constants, such as 0.0, 0.5, 1.0, pi, etc. I'm looking for a solution to make class instantiation work on different literals depending on its type.

Whether or not use float and double literals?

Talk went slightly on the topic whether or not bother to use separate literals for float and double. This might be caused by a bit unfortunate example I gave in my question. In the example literal will be converted to proper type at compile time anyway, so no harm done. But in general there will be cases where literal needs to be used in expression, for example:

float foo(float x)
{
    return x * 3.14;
}

This will force compiler to convert x to double, make computations, then convert result back to float. Pros and cons of such behavior:

Pros:

  • Precision gain because actual computations will be done in double precision.

Cons:

  • If performance is an issue, this might result in faster as well as slower execution, depending on environment and platform. This introduces some performance change depending on implementation, which is as far as I'm considered, bad.
  • Introduces computations inconsistency because some operations will be done on floats and some on doubles, depending on literals use. This might also open some bugs exposure.
  • It breaks the idea to specialize class for floats in the first place because internally computations will be done on doubles anyway.

In conclusion, the goal is to make class instantiation work on proper types without any additional conversions. The idea to use double type literals everywhere, such as 0.5 and leave handling proper conversion to compiler is not an option.

More on the subject: Should we generally use float literals for floats instead of the simpler double literals?

Possible solutions?

  • Specialize methods for every template instantiation type where literals have to be used. This is probably worst solutions because it forces to write the same code twice with minor changes to the literals.
  • Make specialized getValue<type>() methods, or as Jonathan Wakely posted - specialized members. This might lead to have some silly named members or methods in the class, such as getZeroPointFive<float>().
  • Mohammad and Tony Delroy pointed out that wrapping every literal with static_cast<T>() will do the trick. Conversion to proper type will be done at compile time.
2
  • A better solution has come up... comment from Mohammad... casting the values before they partake in expressions ala static_cast<T>(5.0). I know you don't like it, but it's going to be compile time and is localised with the number, whereas getZeroPointFive etc isn't. Jun 20, 2012 at 1:21
  • Thank you for pointing that out. I've added it to possible solutions
    – umebe
    Jun 20, 2012 at 14:37
0

You just don't need to worry about this - use 0.5 and if the type is float the compiler will still initialise it optimally to the same value as if you'd used 0.5f.

It's a bit long-winded, but you might want to read through the "other mechanisms supporting polymorphism" part of my answer here: Polymorphism in c++

For more general usage of the float constant throughout the function - particularly in comparisons and expressions - it's worth reading the link bluemess mentions below....

4
  • Assume that I will use "0.5" literal. Wouldn't that cause implicit (runtime?) conversion to float at some point? In the example I gave it might not be important, but in general I would rather avoid conversions.
    – umebe
    Jun 18, 2012 at 18:17
  • 1
    It would be a compile-time conversion. Jun 18, 2012 at 18:29
  • I have found that using "0.5" literal for float in some cases will lead to multiple conversions which are explained here: Should we generally use float literals for floats instead of the simpler double literals?
    – umebe
    Jun 18, 2012 at 19:09
  • @bluemass: for assignment as shown in the question you can expect it to be done at compile time... (certainly GCC 4.5.3 does that even without optimisation on). For use in other calculations, it certainly can result in conversions to and from double, but generally that's ok as it reduces rounding errors. It can be faster or slower depending on the hardware. Jun 19, 2012 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.