sanisizer
Sanitize sizes to avoid integer overflow
Loading...
Searching...
No Matches
cap.hpp
Go to the documentation of this file.
1#ifndef SANISIZER_CAP_HPP
2#define SANISIZER_CAP_HPP
3
4#include <limits>
5
6#include "comparisons.hpp"
7
13namespace sanisizer {
14
26template<typename Size_, typename Input_>
27constexpr Size_ cap(Input_ x) {
28 constexpr Size_ maxed = std::numeric_limits<Size_>::max();
29 if (is_greater_than(x, maxed)) {
30 return maxed;
31 } else {
32 return x;
33 }
34}
35
36}
37
38#endif
Signedness-safe integer comparisons.
Sanitize sizes to avoid integer overflow.
Definition arithmetic.hpp:14
constexpr Size_ cap(Input_ x)
Definition cap.hpp:27
constexpr bool is_greater_than(Left_ l, Right_ r)
Definition comparisons.hpp:65