Skip to content

Commit 557b444

Browse files
committed
Fix ambiguous clamp
C++17 introduced `std::clamp` which makes passing a type from std namespace (such as chrono-types) ambiguous in the semi-recursive call. Use constexpr-ifs and `std::clamp` in the final path.
1 parent 6277878 commit 557b444

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

libs/common/include/helpers/mathFuncs.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

55
#pragma once
66

77
#include "RTTR_Assert.h"
8+
#include <algorithm>
89
#include <cmath>
910
#include <type_traits>
1011

@@ -22,26 +23,15 @@ constexpr unsigned divCeil(unsigned dividend, unsigned divisor) noexcept
2223
return (dividend + divisor - 1) / divisor;
2324
}
2425
/// Clamp the value into [min, max]
25-
template<typename T>
26-
constexpr T clamp(T val, T min, T max) noexcept
27-
{
28-
if(val <= min)
29-
return min;
30-
else if(val >= max)
31-
return max;
32-
else
33-
return val;
34-
}
3526
template<typename T, typename U>
3627
constexpr U clamp(T val, U min, U max) noexcept
3728
{
38-
using Common = std::common_type_t<T, U>;
39-
if(std::is_signed_v<T> && !std::is_signed_v<U>)
29+
if constexpr(std::is_signed_v<T> && !std::is_signed_v<U>)
4030
{
4131
// min/max is unsigned -> No negative values possible
4232
if(val < 0)
4333
return min;
44-
} else if(!std::is_signed_v<T> && std::is_signed_v<U>)
34+
} else if constexpr(!std::is_signed_v<T> && std::is_signed_v<U>)
4535
{
4636
// min/max is signed
4737
if(max < 0)
@@ -50,7 +40,8 @@ constexpr U clamp(T val, U min, U max) noexcept
5040
min = 0;
5141
}
5242
// Here all values are positive or have the same signedness
53-
return static_cast<U>(clamp(static_cast<Common>(val), static_cast<Common>(min), static_cast<Common>(max)));
43+
using Common = std::common_type_t<T, U>;
44+
return static_cast<U>(std::clamp(static_cast<Common>(val), static_cast<Common>(min), static_cast<Common>(max)));
5445
}
5546
/// Linear interpolation between [startVal, endVal]. Difference between those 2 and elapsedTime should be smallish
5647
template<typename T, typename U, typename V>

0 commit comments

Comments
 (0)