Created
July 10, 2012 21:02
-
-
Save gpakosz/3086209 to your computer and use it in GitHub Desktop.
pre C++11 nullptr anonymous class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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