Boost logo

Boost :

From: Chris Fairles (chris.fairles_at_[hidden])
Date: 2007-07-31 14:22:51


This was a proposed library implementation of nullptr for c++0x from
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1601.pdf

const // this is a const object...
class {
public:
  template<class T> // convertible to any type
    operator T*() const // of null non-member
    { return 0; } // pointer...
  template<class C, class T> // or any type of null
    operator T C::*() const // member pointer...
    { return 0; }
private:
  void operator&() const; // whose address can't be taken
} nullptr = {}; // and whose name is nullptr

Very similar to your impl although I think the static_cast in yours is
redundant. The standard says, 0, being an integral constant expression
that evaluates to zero, is a null pointer constant which is implicitly
convertible to a pointer type (4.10)

Chris

On 7/31/07, Michael Marcin <mike_at_[hidden]> wrote:
> Hello,
>
> I have something like:
>
> class A;
> boost::array<A*,10> the_array;
>
> std::fill( the_array.begin(), the_array.end(), NULL );
>
> Which wont compile (at least under Visual Studio 2005) without a cast
> for NULL... I've had this problem before and decided to try to find an
> easy cast free expressive solution for it.
>
> After a few minutes I came up with:
>
> namespace detail
> {
> struct null_ptr_impl
> {
> template< typename T >
> operator T* () const { return static_cast<T*>(0); }
> };
> } // end namespace detail
> const detail::null_ptr_impl null_ptr;
>
> std::fill( the_array.begin(), the_array.end(), null_ptr );
>
> Does this exist somewhere already? Is it a good solution?
>
> Thanks,
>
> Michael Marcin
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>


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