Skip to content

Instantly share code, notes, and snippets.

@gpakosz
Created July 10, 2012 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpakosz/3086209 to your computer and use it in GitHub Desktop.
Save gpakosz/3086209 to your computer and use it in GitHub Desktop.
pre C++11 nullptr anonymous class
#include <core/Preprocessor.h>
namespace core {
namespace types {
/**
* Anonymous class, to be used instead of <code>0</code> or <code>NULL</code>.
* Enables the selection of the correct form when methods are overloaded for
* both pointers & integrals types.
*/
const class
{
public:
#if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 5) && (__GNUC_PATCHLEVEL < 2)
ALWAYS_INLINE operator void*() const // works around bug #45383
{
return 0;
}
#endif // #if defined(__GNUC__) && (__GNUC__ == 4) && __(__GNUC_MINOR__ == 5) && (__GNUC_PATCHLEVEL < 3)
template<typename T>
ALWAYS_INLINE operator T*() const
{
return 0;
}
template<class C, typename T>
ALWAYS_INLINE operator T C::*() const
{
return 0;
}
private:
void operator &() const; // not implemented on purpose
} null = {}; // the empty brace initializer fills the ISO C++
// (in 8.5 [dcl.ini] paragraph 9) requirements:
//
// If no initializer is specified for an object, and the
// object is of (possibly cv-qualified) non-POD class
// type (or array thereof), the object shall be
// default-initialized; if the object is of
// const-qualified type, the underlying class type shall
// have a user-declared default constructor
} // namespace types
} // namespace core
@gpakosz
Copy link
Author

gpakosz commented Jul 10, 2012

source:

Scott Meyers, "Item 25: Avoid overloading on a pointer and a numerical type", Effective C++: 50 Specific Ways to Improve Your Programs And Designs, 2nd Edition, Addison-Wesley Professional, September 1997, ISBN: 0201924889

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment