N
Neil
I've constructed a custom smart pointer, and for various reasons I'm
in the situation where I'd like to make the raw-pointer ctor for it
explicit.
However, this then disallows assignment from NULL (and implicit
creation, e.g., for 'return NULL' returning a smart pointer). I've
gone out of my way to try to make this smart pointer as drop-in-
replacement friendly as possible, so losing the ability to easily
assign NULL is a bit of a blow.
I *thought* I had a nice work-around by defining an implicit (int)
ctor, and while VS accepts this, g++ (-Wall -pedantic; probably quite
rightly for most cases) reports a warning:
warning: passing NULL to non-pointer argument ...
Can anyone think of a clever way of working around this (even if
it's g++-specific)?
A point of interest may be that '#pragma GCC system_header' can
disable that warning, but only if the actual conversion from NULL to
smart-ptr is in a header with the pragma, and I couldn't figure out
how to wangle that.
Here's a really basic example:
#include <iostream>
template <typename T>
struct Ptr
{
Ptr( const Ptr & ) {} // copy ctor
explicit Ptr( T * ) {} // only explicit from c-ptr
Ptr( int ) {} // to catch NULL (don't care about non 0)
};
int main()
{
float f1 = 0.0;
Ptr<float> p1(&f1); // OK
Ptr<float> p2(NULL); // warning :-(
return 0;
}
in the situation where I'd like to make the raw-pointer ctor for it
explicit.
However, this then disallows assignment from NULL (and implicit
creation, e.g., for 'return NULL' returning a smart pointer). I've
gone out of my way to try to make this smart pointer as drop-in-
replacement friendly as possible, so losing the ability to easily
assign NULL is a bit of a blow.
I *thought* I had a nice work-around by defining an implicit (int)
ctor, and while VS accepts this, g++ (-Wall -pedantic; probably quite
rightly for most cases) reports a warning:
warning: passing NULL to non-pointer argument ...
Can anyone think of a clever way of working around this (even if
it's g++-specific)?
A point of interest may be that '#pragma GCC system_header' can
disable that warning, but only if the actual conversion from NULL to
smart-ptr is in a header with the pragma, and I couldn't figure out
how to wangle that.
Here's a really basic example:
#include <iostream>
template <typename T>
struct Ptr
{
Ptr( const Ptr & ) {} // copy ctor
explicit Ptr( T * ) {} // only explicit from c-ptr
Ptr( int ) {} // to catch NULL (don't care about non 0)
};
int main()
{
float f1 = 0.0;
Ptr<float> p1(&f1); // OK
Ptr<float> p2(NULL); // warning :-(
return 0;
}