Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2005-01-22 07:10:07


David B. Held wrote:
> Jason Hise wrote:
>
>> Just wondering if there was any interest in a better null, or if such
>> a thing existed in boost already. The code is relatively simple:
>> [...]
>
> Heh. Have you read what Scott Meyers has to say about this?

Also, the standard committeee paper N1601
(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1601.pdf) by
Herb Sutter and Bjarne Stroustrup:

#ifndef CPP0x_NULLPTR_N1601
#define CPP0x_NULLPTR_N1601

#if(__cplusplus <= 199711) // C++98

// A library implementation of N1601: provide nullptr semantics

namespace std
{

class nullptr_t
{
    void operator&() const; // address of nullptr cannot be taken
public:
    template< typename T >
    operator T *() const // non-member pointer
    {
       return 0;
    }

    template< typename C, typename T >
    operator T C::*() const // member pointer
    {
       return 0;
    }
};

}

//__declspec(selectany) extern
const std::nullptr_t nullptr;

#endif

#endif

where:

#include <iostream>
#include <typeinfo>
#include <nullptr>

void f( char * )
{
    std::cout << "f( char * )\n";
}

void f( int )
{
    std::cout << "f( int )\n";
}

template< typename T >
void g( T t )
{
    std::cout << "g( " << typeid(T).name() << " )\n";
}

int main()
{
    char * ch = nullptr;
    char * ch2 = 0;
    char * ch3 = true ? nullptr : nullptr;
    int n2 = 0;

    if( ch == 0 ) std::cout << "ch == 0\n";
    if( ch == nullptr ) std::cout << "ch == nullptr\n";
    if( ch ) std::cout << "ch\n";

    if( n2 == 0 ) std::cout << "n2 == 0\n";

    if( sizeof( nullptr ) == sizeof( void * ))
       std::cout << "sizeof( nullptr ) == sizeof( void * )\n";

    std::cout << "typeid(nullptr).name() = " << typeid(nullptr).name()
<< '\n';

    try{ throw nullptr; }
    catch( int ){ std::cout << "caught an integer\n"; }
    catch( std::nullptr_t ){ std::cout << "caught null pointer type!\n"; }

    try{ throw 0; }
    catch( std::nullptr_t ){ std::cout << "caught null pointer type!\n"; }
    catch( int ){ std::cout << "caught an integer\n"; }

    f( nullptr ); // calls f( char * )
    f( 0 ); // calls f( int )

    g( 0 ); // T = int
    g( nullptr ); // T = nullptr_t
    g(( float * )nullptr ); // T = float *

# if 0 // error cases:
       char * ch4 = true ? 0 : nullptr;
       int n = nullptr;
       int n3 = true ? nullptr : nullptr;
       int n4 = true ? 0 : nullptr;

       if( n2 == nullptr ) std::cout << "n2 == nullptr\n";
       if( nullptr ) std::cout << "nullptr\n";
       if( nullptr == 0 ) std::cout << "nullptr == 0\n";
       nullptr = 0;
       nullptr + 2;
# endif

    return 0;
}

results in:

ch == 0
ch == nullptr
n2 == 0
typeid(nullptr).name() = class std::nullptr_t
caught null pointer type!
caught an integer
f( char * )
f( int )
g( int )
g( class std::nullptr_t )
g( float * )

Regards,
Reece


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk