On 9/10/2014 8:25 AM, David Rodríguez Ibeas wrote:
> While I am not against the idea, I doubt that this would catch a whole
> lot of issues. In particular, any null pointer that is not known at
> compile time to be a null pointer, and even null pointers of a type
> different than nullptr_t would not be detected. Additionally it raises
> the question of where to stop, should we also add overloads that take
> pointers everywhere? What about interfaces that take pair of iterators?
I think we'd only need to do this in places where a std::nullptr_t could
be implicitly converted to another (non-pointer) type in a way that
causes UB; those are the places that have caused the most confusion for
me. This means that any ordinary function could remain unchanged, but
classes might need a new overload.
> A slightly more useful approach would be to add an attribute
> [[not_null]] to the arguments of the different functions. It would serve
> as explicit documentation that null is not accepted for the programmer,
> and the compiler would be able to produce diagnostics where either
> nullptr or a null pointer of a different type is passed to the function.
This would probably be a better long-term solution, though. I imagine it
would help quite a bit with static analysis as well. I'm not sure what
should happen if [[not_null]] were applied to a non-pointer type, though
(especially when the type is templated and may or may not be a pointer).
Ideally, [[not_null]] would apply to iterators and smart pointers as
well, but that's probably hard to do.
As long as [[not_null]] were specified to emit a compilation error when
passing nullptr, I'd be happy.
- Jim