Article Discussion
On the Tension Between Object-Oriented and Generic Programming in C++
Summary: The author discusses how the use of generic programming in C++ can lead to conflicts with object-oriented design principles. He demonstrates how a technique known as type erasure can often be used to resolve these conflicts. An in-depth example is presented: any_iterator, a type-safe, heterogeneous C++ iterator.
33 posts on 3 pages.      
« Previous 1 2 3 Next »
The ability to add new comments in this discussion is temporarily disabled.
Most recent reply: November 27, 2007 7:35 AM by Eivind
zade
Posts: 2 / Nickname: zadechina / Registered: September 28, 2005 2:50 PM
Re: On the Tension Between Object-Oriented and Generic Programming in C++
November 19, 2007 4:30 AM      
any_iterator use type-erase tech to erase the actal type such as vector::iterator or list::iterator; in that case, it is only a iterator to access the elements in the container.

but yet any_iterator expose the iterator more details such as the iterator_tag. For my option, I want to only access elements with the type user defined. If you want to tell the iterator tag type to the user, you can use the runtime tech such as through tag_type() function.

so for template class any_iterator, the CategoryOrTraversal template argument should not needed; and to the extreme, all the argument except the Value are not needed. code like below:
template< class Value> class any_iterator;

in this case, type-erase tech is used in its extreme.
Thomas
Posts: 14 / Nickname: tmbecker / Registered: October 15, 2007 3:27 PM
Re: On the Tension Between Object-Oriented and Generic Programming in C++
November 25, 2007 4:06 PM      
> so for template class any_iterator, the
> CategoryOrTraversal template argument should not needed;
> and to the extreme, all the argument except the Value are
> not needed.

The point of these template arguments is to ensure that the any_iterator is a Standard-conforming iterator. Among other things, this ensures that the any_iterator works properly with generic algorithms of the kind found in the STL. If you really need to work with iterators that do not have these capabilities, my recommendation would be to avoid the term "iterator." I believe it's best to reserve the term "iterator" for Standard-conforming iterators.
Eivind
Posts: 3 / Nickname: eeklund2 / Registered: January 4, 2006 8:59 PM
Re: On the Tension Between Object-Oriented and Generic Programming in C++
November 27, 2007 7:35 AM      
> Ok, reply to myself:
>
> >They have chosen not to use the term "type erasure" at
> > all,
>
> I'm beginning to understand why some people are not happy
> with the term "type erasure." It does sound a bit like
> "making things typeless." Perhaps the person who posted
> the root message to this thread was a victim of this
> misunderstanding when he likened type erasure to using
> void*. (See, no matter what someone says, there is always
> some value in it.) Hm, what would be a better term? How
> about NIRP, for "non-intrusive runtime polymorphism" :o)

"Type normalization" or "Type canonicalization". The latter is probably better, though it is a mouthful. Using a common abbreviation for canonicalization, it would be "Type c14n", though that's only slightly better.

Eivind.
33 posts on 3 pages.
« Previous 1 2 3 Next »