C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond

Learn How To Use Clamp stdclamp In Modern C++ 17 and Beyond

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,

You can use your own compare function as an extra parameter, it’s syntax is below,

Is there a simple example about the clamp (std::clamp) in modern C++ 17?

Here is a simple usage:

For example if you want to limit temp input between -273.15 to 2000 °C we define clamp as shown below:

Here is a simple example.

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.

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

Learn How To Use Clamp stdclamp In Modern C++ 17 and Beyond C++ logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++11C++14C++17C++20Learn C++

What Is The Priority Queue (std::priority_queue) In Modern C++?

C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?