4 ms·
Didn’t c++ solve this problem with references vs pointers? References shouldn’t be null, pointers could be.
by ant6n 9mo ago
Didn’t c++ solve this problem with references vs pointers? References shouldn’t be null, pointers could be.
- mojuba 9mo agoIt didn't make pointers safer to use though. In Swift and some other modern languages you can't dereference an optional (nullable) pointer without force-unwrapping it.
- amelius 9mo agoYou can still fabricate a null reference IIRC.
- Rexxar 9mo agoIt always involve an undefined behaviour at creation of the 'null reference', there is no legal way to create it.
- amelius 9mo agoTrue. In any case references solve only half of the problem because it lets you state "this function will not take a null pointer". You still cannot say "this function may take a null pointer" unless you use a very unusual convention of saying that any pointer argument may take a null pointer.
- widdershins 9mo agoI don't find that convention unusual. That's how I (and everyone at my company) writes code every day. If an argument is a pointer, that means it may be null. If it may not be null, it should be a reference.
- amelius 9mo agoAnd the libraries that you use? E.g., std::size_t std::strlen(const char* str);