Is there a nice way to assign stdminmaxa, b to stdtiea, b?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Assigning the result of std::minmax(a, b) directly to std::tie(a, b) to sort two variables in place seems intuitive, but it contains a subtle bug: std::minmax returns references to its arguments, and assigning through std::tie modifies those same variables during the assignment, causing undefined behavior. C++17 structured bindings and careful use of temporaries provide safe alternatives.
The Naive (Broken) Approach
This looks clean but is broken. std::minmax(a, b) returns std::pair<const int&, const int&> — references to a and b. When std::tie(a, b) assigns the first element (the min), it modifies a. Now the second reference (the max) may point to the already-modified value.
Why It Fails
Step by step with a = 7, b = 3:
std::minmax(a, b)returns{ref to b (3), ref to a (7)}std::tie(a, b)assigns first:a = 3(from ref to b) — OK so farstd::tie(a, b)assigns second:b = ref to a— butais now3, sob = 3- Result:
a = 3, b = 3— wrong! Expecteda = 3, b = 7
Safe Solutions
Solution 1: Use std::minmax with an Initializer List
The initializer list version copies the values into a temporary std::initializer_list, breaking the reference chain.
Solution 2: Simple if-swap
For just two variables, the simplest and most readable approach:
This is zero-overhead, completely safe, and communicates intent clearly.
Solution 3: std::tie with Explicit Copies
Force copies before assignment:
std::make_pair creates a pair<int, int> (values, not references), so the assignment is safe.
Solution 4: Structured Bindings with a Temporary
Understanding std::minmax Return Types
The key to understanding the bug is knowing what std::minmax returns:
The two-argument version returns references for efficiency. The initializer list version returns values because the initializer list is temporary.
Comparison of Approaches
| Approach | Safe? | Copies | Readability |
std::tie(a,b) = std::minmax(a,b) | No | 0 | Looks clean but broken |
auto [lo,hi] = std::minmax({a,b}) | Yes | 2 | Good |
if (a > b) std::swap(a, b) | Yes | 0-1 | Best |
std::tie(a,b) = std::make_pair(min,max) | Yes | 2 | Verbose but safe |
Common Pitfalls
- Dangling references:
std::minmaxwith two arguments returns references. If the arguments are temporaries, the references dangle immediately:auto [lo, hi] = std::minmax(getA(), getB());is undefined behavior. - Structured bindings with references:
auto& [lo, hi] = std::minmax(a, b)binds references toaandb. Modifyingaorblater changes whatloandhisee. - Floating-point NaN:
std::minmaxwith NaN values has implementation-defined behavior. NaN comparisons always return false. - C++20 ranges::minmax:
std::ranges::minmaxreturns aranges::minmax_resultstruct. The same reference aliasing issue applies to the two-argument overload.
Summary
std::tie(a, b) = std::minmax(a, b)is undefined behavior becauseminmaxreturns references that alias the assignment targets- Use
if (a > b) std::swap(a, b)for the simplest, safest two-variable sort - Use
std::minmax({a, b})(initializer list) if you need both min and max as values - The root cause is that
std::minmax(a, b)returnspair<const T&, const T&>, notpair<T, T>
Related reading
- Is there a training example of using Tensorflow C API?
- Is there a way to implement analog of Python's 'separator'.join in C?
- Is there a way to simulate the C 'friend' concept in Java?
- Is there an Objective-C algorithm like transform of the C STL?
- Is there any technical reason why std::lower_bound is not specialized for red-black tree iterators?
- Is there anything like stdand or stdor?
- Is two pointer problem same as sliding window
- I''ve heard i isn''t thread safe, is i thread-safe?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.