GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::clamp(3) C++ Standard Libary std::clamp(3)

std::clamp - std::clamp


Defined in header <algorithm>
template<class T> (1) (since C++17)
constexpr const T& clamp( const T& v, const T& lo, const T& hi );
template<class T, class Compare>
constexpr const T& clamp( const T& v, const T& lo, const T& hi, (2) (since C++17)
Compare comp );


1) If v compares less than lo, returns lo; otherwise if hi compares less than v,
returns hi; otherwise returns v.
Uses operator< to compare the values.
2) Same as (1), but uses comp to compare the values.


The behavior is undefined if the value of lo is greater than hi.


v - the value to clamp
lo,hi - the boundaries to clamp v to
comparison function object (i.e. an object that satisfies the requirements
of Compare) which returns true if the first argument is less than the
second.


The signature of the comparison function should be equivalent to the
following:


bool cmp(const Type1 &a, const Type2 &b);
comp -
While the signature does not need to have const &, the function must not
modify the objects passed to it and must be able to accept all values of
type (possibly const) Type1 and Type2 regardless of value category (thus,
Type1 & is not allowed
, nor is Type1 unless for Type1 a move is equivalent to a copy
(since C++11)).
The types Type1 and Type2 must be such that an object of type T can be
implicitly converted to both of them.


-
T must meet the requirements of LessThanComparable in order to use overloads (1).
However, if NaN is avoided, T can be a floating-point type.


Reference to lo if v is less than lo, reference to hi if hi is less than v,
otherwise reference to v.


At most two comparisons.


template<class T>
constexpr const T& clamp( const T& v, const T& lo, const T& hi )
{
return clamp(v, lo, hi, less{});
}


template<class T, class Compare>
constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp )
{
return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
}


Capturing the result of std::clamp by reference produces a dangling reference if one
of the parameters is a temporary and that parameter is returned:


int n = -1;
const int& r = std::clamp(n, 0, 255);
// r is dangling


If v compares equivalent to either bound, returns a reference to v, not the bound.


Feature-test macro: __cpp_lib_clamp

// Run this code


#include <cstdint>
#include <algorithm>
#include <iostream>
#include <iomanip>


int main()
{
std::cout << " raw clamped to int8_t clamped to uint8_t\n";
for(int const v: {-129, -128, -1, 0, 42, 127, 128, 255, 256}) {
std::cout << std::setw(04) << v
<< std::setw(20) << std::clamp(v, INT8_MIN, INT8_MAX)
<< std::setw(21) << std::clamp(v, 0, UINT8_MAX) << '\n';
}
}


raw clamped to int8_t clamped to uint8_t
-129 -128 0
-128 -128 0
-1 -1 0
0 0 0
42 42 42
127 127 127
128 127 128
255 127 255
256 127 255


min returns the smaller of the given values
(function template)
max returns the greater of the given values
(function template)
in_range checks if an integer value is in the range of a given integer type
(C++20) (function template)
ranges::clamp clamps a value between a pair of boundary values
(C++20) (niebloid)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.