Monday, November 6, 2017

C++17: std::clamp()

C++17 added the template function std::clamp() is a combination of std::max() and std::min(). Here is an example:

#include <algorithm>
#include <iostream>

int main()
{
  //                      Value Min Max
  std::cout << std::clamp( 5   ,  0, 10) << " "; //  5
  std::cout << std::clamp(-5   ,  0, 10) << " "; //  0
  std::cout << std::clamp(20   ,  0, 10) << " "; // 10

  std::cout << std::endl;
  return 0;
}
// Output: 5 0 10
Reference: https://stackoverflow.com/questions/38060436/what-are-the-new-features-in-c17

No comments:

Post a Comment