Re: [Boost-users] 'standard' free functions for operators, etc

From: "Peter Dimov"
intrusive_ptr_add_ref is passed a pointer to a class with an embedded reference count. It doesn't matter how many typedefs resolve to X*, X still has its reference count that needs to be incremented. You only run into the problem when "assign" is underspecified and overloaded to mean different things in different contexts.
I understand your point, and maybe it is not a problem for intrusive_ptr in all pratical cases, but intrusive_ptr_add_ref is passed a T *. T may or may NOT be a class. ie T could be an int or an HWND (which is often just void *, depending on #define STRICT) or some other handle, or whatever. Maybe all those examples are a bit ugly - ie the int would have to be something like an index into a global array, or something like that. But it is not necessarily a distinct 'class' that holds its refcount intrinsicly. Or maybe it is necessarily that, but only because of how intrusive_ptr_add_ref was done, not for any other reason. So intrusive_ptr is ever so slightly more restrictive than it need be. Probably not a big deal. Note also, that for those few strange cases, you could also just wrap the HWND or whatever into a class, but then of course, intrusive_ptr could have just required the class to have a specific add_ref member function (and then you might need to wrap all classes). So maybe intrusive_ptr is just an example and not necessarily the best one, but I still wonder 1. if policy based could have been the same for 95% of the cases, and better for the last 5%, 2. there are any other techniques to consider. Either for intrusive_ptr or boost iterators or other places where I want to use the same techniques. I think that's the real question - if I am going to borrow some idioms for boost or wherever, I'd like to borrow the right ones for the right situations. Tony

Gottlob Frege wrote:
From: "Peter Dimov"
intrusive_ptr_add_ref is passed a pointer to a class with an embedded reference count. It doesn't matter how many typedefs resolve to X*, X still has its reference count that needs to be incremented. You only run into the problem when "assign" is underspecified and overloaded to mean different things in different contexts.
I understand your point, and maybe it is not a problem for intrusive_ptr in all pratical cases, but intrusive_ptr_add_ref is passed a T *. T may or may NOT be a class. ie T could be an int or an HWND (which is often just void *, depending on #define STRICT) or some other handle, or whatever.
It doesn't matter. The point is that the type of an entity (by definition) determines the legal operations on the entity, and the effects of these operations. (To be precise, it's actually the reverse.) In other words, if you have an operation F, and two type aliases X and Y that both resolve to 'int' and you want F to have different semantics when applied to X and Y, you are effectively not using the C++ type system. You have imposed your own "logical type system" that is based on names (X and Y) and not types. If you need different (logical) types, you have to use different (physical) types. typedef does not introduce a type. Similarly, if you want to not be able to add meters and kilograms, you have to make meters and kilograms distinct types. You can't solve this fundamental problem by making operator+ policy-based, because in 5 + 6, you have no way to know whether the 5 is 5m or 5kg.
participants (2)
-
Gottlob Frege
-
Peter Dimov