Boost logo

Boost :

From: Dietmar Kuehl (dietmar_kuehl_at_[hidden])
Date: 2002-03-03 15:06:13

Jeff Garland wrote:

> I'm not sure I see the "much more" aspect. And unfortunately this really goes
> against a tide of "OO" think that exists in all sorts of libraries including the
> standard library.

OO is for data structures. I think we have now enough experience with
data structure that we may move our attention to algorithms instead and
this is where Generic Programming comes into play: Algorithms are best
expressed in generic form, eg. by defining them in terms of iterators
over an underlying data structure which is implemented using OO (or
whatever) approaches).

Now, just consider sorting a table with ten columns according to the
first column: It takes two properties to do this, namely the first
column and the property maintaining rows. 'std::sort()' can only cope
with the case where both are identical and using appropriate functors
for the comparison we can help ourselves in many but not all cases.
If 'std::sort()' would take two property maps as additional arguments
(at least optionally), we could eg. easily sort two 'std::vector's
according to a comparison defined on one of them.

> That is, I don't expect to see much of this kind of code:
> std::string foo("bar");
> std::cout << get<size>(foo) << std::endl;
> any time soon.

Nor do I! The code would, of course, look like this:

     std::cout << get(size, foo) << std::endl;

:-) Seriously, however, property maps are mostly for the benefit of
generic code. They also help if more than one attribute is associated
with a position (as is the case for directory iterators) or to get
const correctness right when passing around sequence access facilities
(made up of iterators and property maps). ... and, to stick to your
example of obtaining the size: The defaults eg. for 'std::sort()' would
be the identity. However, you could sort according to the string's size
rather than the contents by providing a 'size' property map:

   std::vector<std::string> strings(...);
   std::sort(strings.begin(), strings.end(), size<std::string>());

where 'size' would be defined like this:

   template <typename T>
   struct size {
     typedef typename T::size_type value_type;
     typedef T key_type;
     typedef readable_property_map_tag category;
   template <typename T>
   typename size<T>::value_type get(size<T> const&, T const& obj) {
     return obj.size();

Maybe it would even be possible to define things in such a way that
it is possible to drop the template argument to 'size<T>'...

But this discussion should go under the label "generalized algorithms
using property maps" rather than "filesystem attributes" :-)

<mailto:dietmar_kuehl_at_[hidden]> <>
Phaidros eaSE - Easy Software Engineering: <>

Boost list run by bdawes at, gregod at, cpdaniel at, john at