Boost logo

Boost :

From: christopher diggins (cdiggins_at_[hidden])
Date: 2005-01-10 21:28:01


----- Original Message -----
From: "Jonathan Turkanis" <technews_at_[hidden]>
> christopher diggins wrote:
>> ----- Original Message -----
>> From: "Jonathan Turkanis" <technews_at_[hidden]>
>
> I'm happy to add classes to make the library easier to use even if
> efficiency is
> sacrificed. one_step_filter is an example of this. I'm just looking for an
> example where it would be easier to write a filter as above than to use
> one of
> the existing filters concepts.

Writing, learning, understanding and reading concepts is hard for
inexperienced (and some experienced) C++ programmers. I still get confused
and frustrated when using them, and I am not completely wet behind the ears.
It took me a couple of hours to figure out how to write that trivial
extension to your library, despite the fact that the library and
documentation is extremely well written.

> Not conforming programs: main must return int. Also, you can't *use* the
> main
> function. E.g.,
>
> void f()
> {
> return main(); // error.
> }

I realize that. I meant that many of these programs can easily have their
guts placed in void functions.

void DoWork() {
  // do work
}

int main() {
  DoWork();
}

By taking this one simple step, a person's code could then be easily reused.
This does overlook the fact that most filter programs take parameters which
is trivially remedied.

>> The only reason to ever choose a over b or c, is simplicity and ease
>> of use.
>
> I understand that's what you're arguing, and it's a perfectly acceptable
> justification. But I haven't seen an example yet.

Consider the following program:

int main() {
  char c;
  while (cin.get(c)) cout.put(toupper(c));
  return 0;
}

Now let's say that after the fact, I want to reuse my code as a filter,
first using [c], I would have to rewrite it as:

struct ToUpper {
  template<typename Source, typename Sink>
  void filter(Source& src, Sink& snk) {
    char c;
    while (src.get(c)) snk.put(toupper(c));
  }
};

Notice the introduction of five new identifiers (filter, Source, src, Sink,
snk) and the fact that the while line has to be rewritten. Now compare that
to using [a], where I simply put the two lines in a void function:

void ToUpper() {
  char c;
  while (cin.get(c)) cout.put(toupper(c));
}

I think it is clear that this would be simpler to teach, document, read,
write and refactor.

> The expression
>
> filter1() | filter2() [A]

Sorry, I thought it was a statement. Wouldn't it be useful to also allow one
liners with a separate syntax:

source() > filter1() > filter2() > sink();

Christopher Diggins
Object Oriented Template Library (OOTL)
http://www.ootl.org


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk