[openbeos] Re: Coding Suggestion -- Templates, Iterators

  • From: François Revol <revol@xxxxxxx>
  • To: openbeos@xxxxxxxxxxxxx
  • Date: Sat, 27 Oct 2001 21:27:25 +0200 (MEST)

Not to start a flame war, but I heard templates had threadsafety problems.
Btw, the only thing in NPlay that uses an template (the list of available
renderers, a templatized BList), I had to comment out the delete fRendererList; 
because it crashed everytime... so I'd say my experience with templates is 
quite short :))

François.

En réponse à Max Jenius <maxjenius22@xxxxxxxxxxx>:

> 
> 
> Your loop macro doesn't work.  (elem++)
> And anyway, it doesn't get rid of the iterator declaration.
> That is more effort than it is worth.
> 
> -Max
> 
> ----- Original Message ----- 
> From: "Daniel Reinhold" <danielr@xxxxxxxxxxxxx>
> To: "OpenBeOS mailing list" <openbeos@xxxxxxxxxxxxx>
> Sent: Saturday, October 27, 2001 1:47 PM
> Subject: [openbeos] Coding Suggestion -- Templates, Iterators
> 
> 
> > 
> > At the risk (ah heck, certainty) of starting a language war, I have
> a
> > couple of suggestion regarding coding style with C++.
> > 
> > I came across the following code today (actually its from CppTest):
> > 
> > void TestSuite::run (TestResult *result)
> > {
> >     for (std::vector<Test *>::iterator it = m_tests.begin ();
> >             it != m_tests.end ();
> >             ++it) {
> > // loop code
> >     }
> > 
> > }
> > 
> > (I've snipped out the loop code)
> > 
> > A couple of things occur to me as I stare at this code (besides the
> > fact that I'm not crazy about C++). One is that the template
> > declaration syntax is often extremely awkward to read. In many
> cases,
> > it makes sense to typedef these buggers into something more
> friendly.
> > In this example, I would do:
> > 
> > typedef std::vector<Test *>::iterator TestIterator;
> > 
> > Another thing is that iterators are pretty common, so I think having
> a
> > standard macro for traversing with one would be very useful. Here's
> my
> > suggestion:
> > 
> > #define foreach(elem,set) for (elem = set.begin (); elem != set.end
> ();
> > ++elem)
> > 
> > Oh, I know, I can hear the groaning out there... "we don't use
> #define
> > macros -- that's old-style, icky C crap that we sophisticated C++
> > programmers don't use". Maybe, but I still think macros are
> extremely
> > useful when used sparingly and judiciously. They're awfully good at
> > hiding uglies.
> > 
> > Now using those two code snippets above, I can rewrite the example
> > (using OpenTracker formatting) as follows:
> > 
> > void
> > TestSuite::run (TestResult *result)
> > {
> > TestIterator i;
> > foreach (i, m_tests) {
> > // loop code
> > }
> > }
> > 
> > Now that's a bit cleaner/clearer. When reading the iterator loop,
> you
> > have to mentally put the word "in" in there. IOW, read it as "for
> each
> > i in m_tests".
> > 
> > So whadya think? Love it? Hate it?
> > 
> > 
> 
> 





Other related posts: