It could be hard to pick the right template instantitiation, because of possible roundoff errors .
Consider the following:
template<float n>
void f(n) {...} //Version 1
template<0.3333>
void f() { ...} // Version 2:Specialization for 0.3333
f(1/3);
-> Which version would be called?
Consider the following code:
template <float f> class foo { ... };
foo<1E6 + 1E-6> my_foo;
" What should the compiler generate? The compiler has to know about the details of target
floating-point architecture to be able to run instantiate the template.
This is easy enough if the compiler is running on the target
architecture, it can just do the calculation and find out the answer, but
if you are cross-compiling, the compiler would have to be able to synthesise
the floating point behaviour of every intended target architecture. And
thankfully the Standards Committee decided that this would be unreasonable. "
Shamelessly copied from here.
Why template non type parameters can not be of class type
As per my understanding a non-type paramater cannot be of class type because there may be more than one implementation of a class. For example
template <typename T>
class demo{...};
template <>
class demo<int>{...};
template <typename T, demo d> //which demo?? demo<T> or demo<int>
class Example{...};
Local classes cannot be used as template parameters because they don't have external linkage.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…