Re: nullptr placeholder without #define
<marcin.sfider@gmail.com> wrote:
I'm a little alergic to #define so when I wanted to make myself a
placeholder
for nullptr keyword for C++0x compability I came up with something
like this:
[snip]
It does not pass only one from Basic Cases from n2431:
if( nullptr == 0 ); // error
On GCC 4.3.3 it compiles.
On MinGW-GCC 3.4.5 it crushes the compiler.
These are GCC issues. Comeau C++ rightly rejects that with a reasonable
error.
Indeed, all the test cases in N2431 (basic and advanced) pass with your
implementation under Comeau C++.
One minor nit. You should add a member of type "void* const" to the class,
preferably with a implementation reserved name like _Val. (Yes, this is not
allowed by the standard, but no real implementation should have issue with
that, and if anybody tries to access the member the use of such naming
should make it clear that something is wrong).
By doing that, you gain sizeof(nullptr)==sizeof(void*) as required in the
draft, without adding any other overhead.
In summary:
const struct nullptr_t {
template<class T>
operator T*() const { return 0; }
template<class T, class U>
operator T U::*() const { return 0; }
void* const _Val; //NEW
} nullptr = {};
(keep the operator== overloads of course).
Then the only issue left is the fact that nullptr_t really belongs in
namespace std.
Since (despite the standard) most compilers support users adding things to
namespace std at the slight risk of breaking things (which this would most
likely not do), I would wrap all that in a "namespace std{/*...*/}" block,
and add a "using ::std::nullptr;" statement at the end.
I think the combination of all of that simulates nullptr as closely as
possible in (real world) C++03.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]