【发布时间】:2019-11-25 12:51:10
【问题描述】:
在我的代码中,我有很多模板算法,其中模板类型必须是浮点数(float、double 或 long double)。其中一些算法需要默认的 epsilon 值。示例:
template <typename FloatType>
bool approx(FloatType x1, FloatType x2)
{
const FloatType epsilon; // How can I set it ?
return abs(x2 - x1) < epsilon;
}
我该如何定义它?我尝试了以下方法,它被 gcc 接受,但它不是标准的(并且在 C++11 中无效)。我知道在 C++11 中是可能的,但我必须与 c++03 兼容。
template <typename FloatType>
struct default_epsilon
{};
template <>
struct default_epsilon<float>
{
static const float value = 1.0e-05f;
};
template <>
struct default_epsilon<double>
{
static const double value = 1.0e-10;
};
template <>
struct default_epsilon<long double>
{
static const long double value = 1.0e-12l;
};
// Then, I use it like that :
template <typename FloatType>
bool approx(FloatType x1, FloatType x2)
{
return abs(x2 - x1) < default_epsilon<FloatType>::value;
}
// or that
bool approx(FloatType x1, FloatType x2, FloatType epsilon = default_epsilon<FloatType>::value)
{
return abs(x2 - x1) < epsilon;
}
【问题讨论】:
-
像 numeric_limits 一样,使用(内联)函数。
-
但这不是标准的 为什么? 我必须与 c++03 兼容。 自 C++11 以来,C++ 中没有模板特化,它也在 C++03 中。
-
@AProgrammer,这不是我喜欢的解决方案,因为我不能将它用作函数声明中的默认参数值。
-
@Caduchon 所以在类定义之外初始化那些静态常量。
-
@Caduchon,为什么你认为你不能使用函数调用作为默认参数? AFAIK,这从来都不是真的。
标签: c++ templates constants c++03