Problem with gcc nullptr

I am migrating existing code to compile under gcc 4.7.2 and have run into a strange problem with nullptr. I managed to weld it to a simple test case:

#include <stdio.h> const char* g_marker = "Original value"; void SetMarker( const char* s ) { g_marker = s; } char* Test1() { return SetMarker( "I was here 1" ), nullptr; } char* Test2() { SetMarker( "I was here 2" ); return nullptr; } char* Test3() { return SetMarker( "I was here 3"), (char*)NULL; } int main() { char* returnValue = Test1(); printf( "%s\n", g_marker ); } 

Compile this with g ++ test.cpp -o test -std = C ++ 0x.

The result I would expect is "I was here 1", but I get a "Raw value" indicating that SetMarker is never called.

Calling Test2 or Test3 gives the expected result.

The code I'm working with uses the pattern observed in Test3 - initially without casting to NULL - which gave an error when converting from int to char * incorrectly, so I started changing all of these NULLs to nullptr, Unfortunately, this just leads incorrectly yourself.

I most likely have to change the code to use the template in Test2 (which I prefer anyway), but I'm curious to know if this is a compiler error or something is missing.

+6
source share
1 answer

This is a bug in g ++: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52988

g ++ discarded side effects in nullptr_t expressions based on the assumption that all nullptr_t values nullptr_t equivalent (they are, but that doesn't mean you can ignore side effects!)

This is fixed in version 4.8.0; new versions on branches 4.x (4.6.4 and 4.7.3) should also have a fix.

+7
source

All Articles