-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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.
> 
> 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. Over Python... I'd have to agree that C++ probably only
makes sense for specific circumstances. My point with the language is
expressive enough that you can define new runtime heuristics and
incorporate them in to your code such that notions like "C++ sucks
because the only way to make sure I don't dereference null pointers all
the time is to manually code a check each time I do a dereference" is
neither fair nor accurate.

> And what does this do to compilation speed? Runtime bloat? Cognitive
> load on the programmer?

I've never measured it, but I imagine it has a very mild impact on
compilation speed vs. using raw pointers. For a good compiler, the
runtime cost for this should be no more than the cost of adding the null
pointer check manually in each place in the code (this is the joy of
templates: abstractions that don't have to cost you anything at
runtime), and probably the same more or less as the equivalent runtime
bloat in a language that has built in checking for this stuff. As for
the cognitive load on the programmer.... writing a good template library
does require some additional cognitive load, although not necessarily
any more than writing a good OO library. Using one tends to require very
little additional overhead (indeed, you generally end up with a net
reduction in cognitive load). I don't see why knowing that you've got
guaranteed checks prior to dereferencing in C++ ought to add more
cognitive load than knowing that in some other language.

- --Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGb6c7OagjPOywMBARAmvbAJ47SqzdWYVNR/KtPgYzUXlfKc1BHACfTqsr
VWGgrikWUUh2IyGRoyEVZGM=
=dChZ
-----END PGP SIGNATURE-----

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

Reply via email to