Iterator facade example could use some changes/documentation adjustments

My example here: http://codepad.org/pH3pXAvb As mentioned in the comment block at the codepad page: http://www.boost.org/doc/libs/1_41_0/libs/iterator/doc/iterator_facade.html#... The above link demonstrates in a linked-list scenario, using is_convertible to handle the conversion from non-const iterators to const iterators. This works fine in the example's linked list scenario, but it's likely than many users of iterator_facade will more generally copy this usage to their own container, from the example. However, as my example demonstrates, in the case where the data lives in contiguous memory locations, or perhaps other scenarios, using is_convertible is very bad for this purpose. It lets you convert from a derived class pointer to a base class pointer, and this is problematic if sizeof(base) is less than sizeof(derived) See the below, very minimal example to illustrate my point. Look at CFixedTest for my proposed replacement for is_convertible to prevent this undesirable behavior. Seems to me that noting such behavior, or simply changing the example code to not allow the conversion (as my example does), would remedy the misunderstanding/problem. Just thought I'd contribute that - thanks, nacitar

Jacob McIntosh wrote:
My example here: http://codepad.org/pH3pXAvb
As mentioned in the comment block at the codepad page: http://www.boost.org/doc/libs/1_41_0/libs/iterator/doc/iterator_facade.html#...
The above link demonstrates in a linked-list scenario, using is_convertible to handle the conversion from non-const iterators to const iterators. This works fine in the example's linked list scenario, but it's likely than many users of iterator_facade will more generally copy this usage to their own container, from the example.
However, as my example demonstrates, in the case where the data lives in contiguous memory locations, or perhaps other scenarios, using is_convertible is very bad for this purpose. It lets you convert from a derived class pointer to a base class pointer, and this is problematic if sizeof(base) is less than sizeof(derived)
See the below, very minimal example to illustrate my point.
Look at CFixedTest for my proposed replacement for is_convertible to prevent this undesirable behavior.
Seems to me that noting such behavior, or simply changing the example code to not allow the conversion (as my example does), would remedy the misunderstanding/problem.
Just thought I'd contribute that - thanks, nacitar
I think this is a valid concern, though I'm guessing this would cause problems at most infrequently. I'm having difficulty coming up with a use case where this would be a problem. Did you stumble upon this while writing "real" code? Would this motivate the existence of an is_iterator_convertible metafunction, which is identical to is_convertible except for pointers? - Jeff
participants (2)
-
Jacob McIntosh
-
Jeffrey Hellrung