Christopher Smith wrote:
Chuck Esterbrook wrote:
On 6/11/07, Christopher Smith <[EMAIL PROTECTED]> wrote:
template <typename T>
class Ptr {
public:
    Ptr(T* a) : ptr(a) {}
    Ptr(Ptr<T>& a) : ptr(a.ptr) {}
    T& operator*() const { return *operator->(); }
    T* operator->() const { if (nullptr == a) { throw
null_pointer_exception(); } return a; }
    bool operator==(const Ptr<T>& a) const { return ptr == a.ptr; }
    Ptr<T>& operator=(const Ptr<T>& a) { ptr = a.ptr; }

private:
    T* ptr;
};

Now you can avoid that error in C++ (not to mention abusing pointer
arithmetic). You're welcome.

DO *NOT* USE THIS CODE!

Go find a library at boost:
http://www.boost.org

WHY???? Why would I prefer that over Java, C# or Python where these
semantics are built in and I'll never forget to type "Ptr(...)"?

If you don't appreciate the advantages over Java and C#, I can't explain
them to you.

You just demonstrated why I now *despise* programming in C++. It isn't just the language; it's the programmers.

Rather than pointing someone to a well-debugged, peer-reviewed, unit-tested smart pointer library (or similar), you knocked something out that has an unclear amount of exception safety. It also probably has quite a few subtle failure modes in multithreaded code because you replaced pointer operations (many of which have atomicity guarantees) with multistep operations.

Worse, in order to figure out even *if* that code has exception problems, I would have to go pick up my copy of "Exceptional C++", read it cover to cover *again*, and stare at that code for quite a while.

Thank you for demonstrating the cognitive load suckage of C++ in concrete form.

-a

--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg

Reply via email to