non-conformant behavior of std::minmax in MSVC 2010 - by Dahua_mit

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 674652 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 6/8/2011 7:45:50 AM
Access Restriction Public


Recently, I started using some standard functions provided by C++0x, which include std::minmax. I am currently using Microsoft Visual Studio 2010 (SP1).

According to the standard [alg.min.max], the function should be declared as

template<class T> std::pair<const T&, const T&> minmax(const T&, const T& );

In other words, it should return a pair of type std::pair<const T&, const T&>. This is also what is said in MSDN document. However, what the MSVC's actual implementation returns is of type std::pair<T, T>.  The codes in the head file <xutility> (line 2107 - 2116) clearly confirms this.

Experienced programmers all understand the potential risk when you expect a reference but what you actually get is a temporary object. If T here is just an integer, it might be ok; but what if T is some big stuff. This would incur inefficiency, and in many cases even undefined behavior.

In addition, in the standard form, T need not to be copyable, but in current implementation provided by MSVC 2010 (SP1), the arguments are copied to the returned pair, implicitly enforcing the constraint that T is copyable.

Non-conformant behavior in such basic functions is very dangerous (more importantly, the actual implementation differs from what is documented in MSDN), which implicitly injects subtle bugs in innocent codes that may take hours to figure out.

Please fix this!  Thank you.
Sign in to post a comment.
Posted by Microsoft on 6/17/2011 at 6:44 PM

Thanks for reporting this bug. We've fixed it, and the fix will be available in VC11. (The correct implementation of minmax() doesn't work yet due to a compiler bug triggered when the pair of references is moved. Our compiler team is investigating.)

If you have any further questions, feel free to E-mail me at .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by MS-Moderator10 [Feedback Moderator] on 6/8/2011 at 10:26 PM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by MS-Moderator01 on 6/8/2011 at 7:54 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(