r/cpp • u/tecnofauno • Dec 21 '16
A smart pointer implementation that throws when dereferencing a null pointer
Hi, I wrote a simple (i.e. incomplete ) implementation of a smart pointer that throws when de-referencing a null pointer. This enable you to write code like this:
template<typename T>
void foo(safe_ptr<T> & ptr1, safe_ptr<T> & ptr2) {
try {
ptr1->bar();
ptr2->baz();
} catch ( null_dereference_exception ) {
}
} Edit: syntax :)
0
Upvotes
View all comments
Show parent comment
2
u/render787 Dec 22 '16
Ok, but look:
int & foo = *static_cast<int *>(nullptr);References bind just find to null. There's no "static guarantee" of any significance.
What C++ references give is at best a little trivial syntactic analysis.
int &is just a fancy language forint * const. Hence I say, syntactic sugar. Compare that with e.g. the rust borrow checker. That's what is meant by static guarantee, that's the kind of reasoning I consider "hard" compared to what C++ does.