The C++17 brings a lot of features to the modern programming. The Parallelism Technical Specification adds several new algorithms to the standard C++ library. These are modernized in the <algorithm>
header in the standard library. In this library there is a very less know useful algorithm std::clamp
, which is an algorithm that returns value in its range. In this post, we explain what is std::clamp
and how we can use it in our modern C++ applications.
What is the clamp (std::clamp) in modern C++ 17?
The clamp (std::clamp
) is a new algorithm that comes with C++17, defined in <algorithm>
header, returns value of given value in range. In other words, you can use std::clamp
to limit your value by the given minimum and maximum values, and the return value will be in this range.
Since C++17, Standard clamp function is defined as below,
1 2 3 |
template< class T > constexpr const T& clamp( const T& val, const T& minval, const T& maxval ); |
You can use your own compare function as an extra parameter, it’s syntax is below,
1 2 3 |
template< class T > constexpr const T& clamp( const T& val, const T& minval, const T& maxval, Compare compr ); |
Is there a simple example about the clamp (std::clamp) in modern C++ 17?
Here is a simple usage:
1 2 3 4 |
int x = 50; int y = std::clamp( x, 0, 100); // returns 50 |
For example if you want to limit temp input between -273.15 to 2000 °C we define clamp as shown below:
1 2 3 |
double temp = std::clamp( 100., -273.15, 2000.); // returns 100. |
Here is a simple example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <algorithm> int main() { int x = 101; int y = std::clamp( x, 0, 100); std::cout << y << std::endl; system("pause"); return 0; } |
Is there a full example about the clamp (std::clamp) in modern C++ 17?
Here is a full example about the clamp (std::clamp
) in modern C++ 17.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> #include <algorithm> bool mycomp(double a, double b) { return (a < b); } int main() { int x = 101; int y = std::clamp( x, 0, 100); std::cout << y << std::endl; double probe_temp; std::cout << "Testing in the range of -273.15 to 1000." << std::endl; probe_temp = 125.2; double temp1 = std::clamp( probe_temp, -273.15, 1000.); std::cout << "Temp (" << probe_temp << " 'C): " << temp1 << "'C" << std::endl; probe_temp = -280; double temp2 = std::clamp( probe_temp, -273.15, 1000.); std::cout << "Temp (" << probe_temp << " 'C): " << temp2 << "'C" << std::endl; probe_temp = 1200; double temp3 = std::clamp( probe_temp, -273.15, 1000.); std::cout << "Temp (" << probe_temp << " 'C): " << temp3 << "'C" << std::endl; system("pause"); return 0; } |
When we are using variables we want them in ranges, especially it is important when you have engineering inputs from user. The best thing is warning user about the wrong given value in range. Sometimes when you analyze data you want to trim some maximum and minimum values, thus you can use clamp to get values that are trim between these values. So be sure that user knows minimum and maximum values.
In visual programming, in C++ Builder, there is a TrackBar (TTrackBar) which has Min and Max parameters to limit your values if you have smaller number of values (i.e. 10 to 100). Moreover, there is a NumberBox (TNumberBox) component which has Min and Max parameters that can be used for any range. Thus, the user knows his/her input remains same or changed in its range.
If you want to know more about new algorithms in C++17, please check this post.
For more details about this clamp feature in C++17 standard, please see this paper; P0025R0
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition