//============================================================================= // drt/sys/snull.hpp // C++ definitions for null pointer constant 'null'. // // Usage // This header provides a 'null' constant that can only be used in // contexts where a constant pointer expression is valid. Specifically, // 'null' cannot be used in contexts where '0' (integer zero) is allowed // (which is how the standard 'NULL' macro behaves). // // Examples // The following are valid uses of the 'null' constant: // // void * pv = null; // int * pi = null; // Type * pt = null; // Type * p2(null); // // if (pi == null) ... // foo(null); // foo((Type *)null); // foo(static_cast(Type *, null)); // // The following are invalid (illegal) uses of the 'null' constant: // // int n = null; // Error // char c = null; // Error // // null = pi; // Error // null = 0; // Error // if (null == 0) ... // Error // // See also // sdefs.hpp // // Acknowledgments // The idea of using a class template member function to implement 'null' // was originated by , circa 1998. See the and // newsgroup archives. // // History // 1.00, 1999-08-19, David R Tribble. // First cut. // // Copyright ©1999, by David R. Tribble, all rights reserved. // See "drt/sys/copyr.txt" for more information. //----------------------------------------------------------------------------- #ifndef drt_sys_snull_hpp #define drt_sys_snull_hpp 100 // Identification #ifndef NO_H_IDENT static const char drt_sys_snull_hpp_id[] = "@(#)drt/sys/snull.hpp 1.00"; #endif // Verify include order #ifndef drt_sys_sdefs_hpp #error Use "sys/sdefs.hpp" instead of this header #endif #if not DRT_HAS_MEMB_TEMPL // This file should not have been #included, see "sdefs.hpp" #error This C++ compiler does not support class member template functions #endif // Local wrappers #include "slib1.hpp" drt_namespace_begin //----------------------------------------------------------------------------- // Class DrtNull // Implements a 'null' constant that can be used like a real null pointer // constant (as a replacement for '0' and 'NULL'). // // Usage // (See the comments at the top of this file.) // // History // 100, 1999-08-19, David R Tribble. // First cut. //----------------------------------------------------------------------------- #define DrtNull_VS 100 // Class version number class DRTEXPORT DrtNull { public: // Functions inline /*void*/ ~DrtNull() { } // Destructor inline /*void*/ DrtNull() { } // Default constructor template inline /* T * */ operator T *() const; // Typecast operator private: // Functions // Constructors and destructors not provided /*void*/ DrtNull(const DrtNull &r); // Copy constructor const DrtNull & operator =(const DrtNull &r); // Assignment operator }; // Inline member functions //----------------------------------------------------------------------------- // DrtNull::operator T *() // Typecast operator, which converts a DrtNull into a null pointer of type // 'T *', i.e., it returns a null pointer to type 'T'. //----------------------------------------------------------------------------- template inline /* T * */ class DrtNull::operator T *() const { return (static_cast(T *, 0)); } //----------------------------------------------------------------------------- // Public constants //----------------------------------------------------------------------------- const DrtNull null; // Generic null pointer constant //----------------------------------------------------------------------------- // Protection declarations //----------------------------------------------------------------------------- #define DrtNull Do_not_use_class_DrtNull.. // Prevents the use of class DrtNull // Wrapper end #include "slib2.hpp" drt_namespace_end #endif // drt_sys_snull_hpp // End snull.hpp