VC++2012 compiler bug (in optimized builds) - by ddoarn

Status : 


Sign in
to vote
ID 776310 Comments
Status Closed Workarounds
Type Bug Repros 3
Opened 1/11/2013 1:29:13 AM
Access Restriction Public


valid C++ source generates wrong code depends on optimization settings and moon phases.
Sign in to post a comment.
Posted by Rui [MSFT] on 8/1/2013 at 5:12 PM
Thank you for following up on this issue. Here is some update on the bug. We have fixed this bug in our compiler and the fix will be shipped in VS2013 RTM.

If you need a workaround in the meanwhile, you may use the workaround provided by Eric previously. Please feel free to let us know if you see any other issue.

Rui Zhang
Microsoft Visual C++
Posted by UnitUniverse on 6/27/2013 at 10:15 PM
It seems that the bug is still there in the VS2013_Preview.
Posted by Eric [MSFT] on 1/17/2013 at 10:18 AM
I am closing this MSConnect item. Feel free to re-activate it if you need anything else.
Posted by Eric [MSFT] on 1/17/2013 at 10:07 AM
Hi, thanks for reporting this. I confirm this bug in the compiler optimizer. We will fix this in a future release.

Unfortunately, there is no easy way to pinpoint a specific line of code to be able to work around this issue. In your code sample, you can workaround the issue by turning off optimizations using #pragma optimize("", off) / #pragma optimize("", on) around main(), or by marking op1 and op2 as __declspec(noinline).

If you feel you would like a hotfix for VS 2012, please visit the support site:;en-us;selectassist&target=assistance

Eric Brumer - Microsoft Visual C++
Posted by Microsoft on 1/16/2013 at 1:03 AM
Hi ddoarn, thanks for your respnose. We have received your attached file. 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 ddoarn on 1/15/2013 at 11:51 PM attached by UnitUniverse is just right test solution. Reproduces only in x64 speed-optimized builds.

Further it was shown that even non-templated version has same problem:

void op1( const unsigned long long &_t1 )
    std::hash<unsigned long long> fn;
    const unsigned long long hash = fn( _t1 );
    printf( "%llu, %llu\n", (unsigned long long)_t1, hash );

void op2( unsigned long long _t1 )
    std::hash<unsigned long long> fn;
    const unsigned long long x = _t1;
    const unsigned long long hash = fn( x );
    printf( "%llu, %llu\n", (unsigned long long)_t1, hash );

int _tmain(int argc, _TCHAR * argv [])

    printf( "\npass 1 (int)\n" );
    for ( unsigned int i = 1; i < 4; ++i )
     op1( i );

    printf( "\npass 2 (int)\n" );
    for ( unsigned int i = 1; i < 4; ++i )
     op2( i );

    printf( "\npass 1 (long long)\n" );
    for ( unsigned long long i = 1; i < 4; ++i )
     op1( i );

    return 0;
Posted by Microsoft on 1/13/2013 at 9:43 PM
Thank you for submitting feedback on Visual Studio and .NET Framework. In order to efficiently investigate and reproduce this issue, we are requesting a demo project. Please submit this information to us within 4 business days. We look forward to hearing from you with this information.
Posted by Microsoft on 1/11/2013 at 1:50 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(