Question: I have a template class that worked perfectly in Visual Studio 6 but fails in a more modern version.

```
template<double B, double C>
class MyClass
{
    double k(double x) const
    {
        x = fabs(x);
        if (x < 1.0)
            return ((2.0 - 1.5*B - C) * x*x*x) + ((-3.0 + 2.0*B + C) * x*x) + (1.0 - 0.33333333*B);
        if (x < 2.0)
            return ((-0.16666667*B - C) * x*x*x) + ((B + 5.0*C) * x*x) + ((-2.0*B - 8.0*C) * x) + (1.3333333*B + 4.0*C);
        return 0.0;
    }
};

error C2993: 'double' : illegal type for non-type template parameter 'B'

```

I get it, the standard disallowed using `double` constants as template parameters and VC++ is finally conforming. But what should I use instead? The expression is evaluated repeatedly and is a true bottleneck, and I'd like the constants to be computed at compile time rather than run time.

Answer: 
The problem is that matching template arguments to the program-wide set of instantiations depends on exact equality, which based on the machine's particular representation of floating-point values may vary from platform to platform. (And since the compiler is not required to provide compile-time emulation of the target machine, may vary between compile time and runtime.)

The solution is to replace `double` with some other kind of unambiguous representation, such as ratios, fractions over a fixed amount such as a million, or your own floating-point style with integer mantissa and exponent.

In C++11, you could apply `constexpr` function evaluation, which in this case would eliminate the templates:

```
struct MyClass {
    constexpr MyClass( double b, double c )
        : b15( b * 1.5 ), b20( b * 2.0 ), b03( b * 0.33333333 ),
        /* etc */

    double b15, b20, b03, /* etc */

    double k( double x ) const {
        x = fabs(x);
        if (x < 1.0)
            return ((2.0 - b15 - C) * x*x*x) + ((-3.0 + b20 + C) * x*x) + (1.0 - b03);
        /* etc */
    }
};

```

This precomputes the constant subexpressions and effectively makes them runtime parameters to a single common function. But the parameter/member object accesses could slow it down.

If the function can always be inlined, that alternative will probably produce similar results to the original template at less effort.
if you use a constexpr function it should do everything you want at the minimal cost of having to use ?: operator since you can only have one return statement:

```
constexpr double k(double B, double C, double x) const
{
    x = fabs(x);
    return 
          (x < 1.0) ? 
               ((2.0 - 1.5*B - C) * x*x*x) + ((-3.0 + 2.0*B + C) * x*x) + (1.0 - 0.33333333*B)
        : (x < 2.0) ?
           ((-0.16666667*B - C) * x*x*x) + ((B + 5.0*C) * x*x) + ((-2.0*B - 8.0*C) * x)
           + 1.3333333*B + 4.0*C)
        : 0.0;
}

```

but that requires that x is a literal (known at compiletime)

It also unfortunately requires that you do not use VS as per

<http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx>

if x is a variable (changes at runtime) or you use VS:

just remove the `constexpr` keyword above. It is likely that most of the optimizations will occur simply from the fact that B and C are known at compiletime - check the asm to verify. (upgrading not only increases strictness, it also likely improved optimization)