Unique_ptr, nullptr and support for gcc 4.5.x and 4.6.x

I am working on a library with two different end users, one of which uses gcc 4.5.3, and the other has just migrated to gcc 4.6.3. The library uses the new C ++ 11 smart pointers (particularly unique_ptr) and compiles in gcc 4.5.3. However, between these two versions, gcc began to support nullptr, so the unique_ptr API has changed more closely according to the standard. However, now the following code has gone from a fine to ambiguous

unique_ptr up( new int( 30 ) );
...
if( up == 0 ) // ambiguous call now to unique_ptr(int) for 0

Is there a clean (namely, the following sentence) way to modify the above if statement so that it works with or without a null value? I would like to avoid a configuration check and then a macro as shown below (which I think will work) if possible

#if defined NULLPOINTER_AVAILABLE
  #define NULLPTR (nullptr)
#else
  #define NULLPTR (0)
#endif

, ?

+5
1

?

#include <iostream>
#include <memory>
int main() {
 using namespace std;
 unique_ptr<int> up( new int( 30 ) );
 if (up == 0)
     cout << "nullptr!\n";
 else cout << "bam!\n";
}

g++ -std=c++0x -Wall nullptr.cpp -o nullptr (gcc 4.6.2).

, N2431 Stroustrup Sutter nullptr, ( 0) .

+3

All Articles