为什么不允许double作为非类型模板参数? [英] Why is double not allowed as a non-type template parameter?
问题描述
2003年-是, 2003 -Vandervoorde和Josuttis在他们的书 C ++模板(第40页)中写道:
In 2003 - yes, 2003 - Vandervoorde and Josuttis wrote this in their book "C++ Templates" (p. 40):
不能将浮点文字(和简单的常量浮点表达式)用作模板参数是有历史原因的。由于没有严重的技术挑战,因此在将来的C ++版本中可能会支持。
Not being able to use floating-point literals (and simple constant floating-point expressions) as template arguments has historical reasons. Because there are no serious technical challenges, this may be supported in future versions of C++.
但这仍然行不通,甚至在C ++ 11下:
But this still doesn't work, even under C++11:
template<double D> //error
void foo() {}
为什么不添加? p>
Why was this not added?
推荐答案
我一直认为它与相互匹配的实现有关。就像这两个实例相同还是不同:
I had always assumed it had to do with matching implementations against each other. Like are these two instances the same or different:
template class foo<10./3.>
template class foo<1./3 * 10.>
它们可能不会生成相同的双精度表示形式,因此编译器可能会将它们视为不同的类。然后您就无法将它们彼此分配,等等。
They may not generate the same double-precision representation, so the compiler might think of them as different classes. Then you couldn't assign them to each other, etc.
这篇关于为什么不允许double作为非类型模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!