[C++] Temporary strings implicitly constructed take l-value path instead of r-value path - by CodingCat

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 669549 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 5/20/2011 1:23:42 AM
Access Restriction Public


Implicitly constructing temporary string objects by passing C-strings to methods or functions that are overloaded for both r-val references and const l-val references (such as any container insertion method) results in calling of the const-l-val-version rather than the r-val-version, even though the string object passed is evidently only a temporary object.

The problem affects more than just strings, but it is particularly annoying for these, as passing C-strings to methods taking string objects is rather common.
In fact, it seems that any referencing of objects existing outside the expression to be implicitly converted inside this expression provokes this issue, e.g. by implicitly calling int_test_class(int i) with a non-temporary integer.
Sign in to post a comment.
Posted by Microsoft on 5/20/2011 at 9:32 PM

Thanks for reporting this bug. We've already fixed it, and the fix will be available in VC11. Surprisingly, VC10's behavior was mandated by the C++0x Working Paper at the time (what I refer to as "rvalue references v2.0"). This was fixed by the Standardization Committee (producing what I refer to as "rvalue references v2.1"), and we've partially implemented that in VC11. Our current implementation is enough to fix your test case. Other cases remain to be fixed, and are already tracked by an internal bug.

I talked about this very issue in this video: http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-4-of-n

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

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by MS-Moderator08 [Feedback Moderator] on 5/20/2011 at 2:15 AM
Thank you for reporting the issue.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.These specialized experts will follow-up with your issue.