我在C#中有一個方法,如下所示(在一個範圍內包裝一個數字,比如0到360 ...如果你傳遞0-359,你會得到相同的值,如果你傳遞360,你得到0,361得到1,等):在C++模板中輸入條件?
/// <summary>
/// Wraps the value across the specified boundary range.
///
/// If the value is in the range <paramref name="min"/> (inclusive) to <paramref name="max"/> (exclusive),
/// <paramref name="value"/> will be returned. If <paramref name="value"/> is equal to <paramref name="max"/>,
/// <paramref name="min"/> will be returned. The method essentially creates a loop between <paramref name="min"/>
/// and <paramref name="max"/>.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="min">The minimum value of the boundary range, inclusive.</param>
/// <param name="max">The maximum value of the boundary range, exclusive.</param>
/// <returns>The value wrapped across the specified range.</returns>
public static T Wrap<T>(T value, T min, T max) where T : IComparable<T>
{
// If it's positive or negative infinity, we just return the minimum, which is the "origin"
bool infinityDouble = typeof(T) == typeof(double) && (double.IsPositiveInfinity(Convert.ToDouble(value)) || double.IsNegativeInfinity(Convert.ToDouble(value)));
bool infinityFloat = typeof(T) == typeof(float) && (float.IsPositiveInfinity(Convert.ToSingle(value)) || float.IsNegativeInfinity(Convert.ToSingle(value)));
if (infinityDouble || infinityFloat)
{
return min;
}
// If the value is between the origin (inclusive) and the maximum value (exclusive), just return the value
if (value.CompareTo(min) >= 0 && value.CompareTo(max) < 0)
{
return value;
}
// The range of the wrapping function
var range = (dynamic)max - (dynamic)min;
return ((((value % range) + range) - min) % range) + min;
}
我還需要在C++這種方法,我定義如下:
/*!
Wraps the value across the specified boundary range.
If the value is in the range \a min (inclusive) to \a max (exclusive), \a value will be returned.
If \a value is equal to \a max, \a min will be returned. The method essentially creates a loop between
\a min and \a max.
\param value The value to wrap.
\param min The minimum value of the boundary range, inclusive.
\param max The maximum value of the boundary range, exclusive.
\return The value wrapped across the specified range.
*/
template <typename T> const T& MathHelper::wrap(const T &value, const T &min, const T &max)
{
// If it's positive or negative infinity, we just return the minimum, which is the "origin"
bool infinityDouble = value == std::numeric_limits<double>::infinity() || value == -std::numeric_limits<double>::infinity();
bool infinityFloat = value == std::numeric_limits<float>::infinity() || value == -std::numeric_limits<float>::infinity();
if (infinityDouble || infinityFloat)
{
return min;
}
// If the value is between the origin (inclusive) and the maximum value (exclusive), just return the value
if (value >= min && value < max)
{
return value;
}
// The range of the wrapping function
T range = max - min;
return ((((value % range) + range) - min) % range) + min;
}
現在我的問題是:我會爲無窮正確的C++版本檢查?我看不出任何方式說「如果加倍,做這些檢查,如果是浮動,做這些檢查」。如果它不是我想要的類型,它會返回false嗎? 另外,爲什麼%運算符未定義爲float和double?我想我必須自己實現模運算符。該方法非常適用於數字類型 - byte,short,int,long,float,double。
「如果雙,做這些檢查,如果浮動,做這些檢查」 - >這是模板專業化是什麼 – Colin 2010-06-23 18:37:34
是啊,我忘了所有關於專業化......但這些都是個別方法需要專門化,所以它更重載而不是專業化。 – 2010-06-23 21:13:05