Namespaces
Variants
Views
Actions

Talk:cpp/algorithm/clamp

From cppreference.com

Here is a working example with the “possible implementation” code snippets inlined: https://ideone.com/IpcDt9

std::independent_bits_engine does not accept signed types. Simplified, keeping the idea. --Cubbi (talk) 06:20, 23 May 2016 (PDT)

For this diff by User:Ybab321, I don't believe it's correct to revert the original change, because std::less<> has specified behavior for comparisons invoking built-in pointer operators, which normal operator less does not. Randomcat (talk) 12:33, 19 February 2021 (PST)

That's a good point that I hadn't considered fully, unreverting... --Ybab321 (talk) 08:26, 21 February 2021 (PST)

[edit] UB if backward?

The page says it's UB if min and max are reversed. The standard doesn't seem to say that (http://open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf page 1047. It does say "The value of lo shall be no greater than hi." BenFrantzDale (talk) 12:30, 18 May 2021 (PDT)

Where does the page say it's UB if min and max are reversed? --Ybab321 (talk) 13:00, 18 May 2021 (PDT)
After "2)" it says "The behavior is undefined if the value of lo is greater than hi." BenFrantzDale (talk) 12:18, 21 May 2021 (PDT)
right, that's what https://eel.is/c++draft/alg.clamp#2 says, in a slightly harder-to-read format --Cubbi (talk) 11:49, 22 May 2021 (PDT)
(Ouch. MUCH harder to read, yeah.) Makes sense, then — if the standard defines a precondition that hi must be greater than lo (or, technically, that hi < lo is false), but doesn't define the behavior when that's not the case, then sort of by definition the behavior is undefined. -- FeRDNYC (talk) 17:40, 24 May 2021 (PDT)