Topic: STL design flaw?


Author: Valentin Bonnard <bonnard@clipper.ens.fr>
Date: 1999/02/07
Raw View
Christopher Eltschka wrote:
>
> James Kuyper wrote:
> >
> > sirwillard@my-dejanews.com wrote:
>
> [...]
>
> > > transform<z_type, y_type, Sum&>(zzz, yyy, x);
> > >
> > > This is more verbose, but should work.

> The point is the last template argument: He explicitly gave a
> _Reference_ for this argument, while the automatically chosen
> specialisation takes just Sum, not Sum&.
>
> An interesting idea. However, I'm not sure if this is guaranteed
> to work by the standard, although I don't see a reason why it
> shouldn't work. Anyone who knows what the standard says about
> this?

I don't see anything in the std which forbids it; OTOH,
SGI STL clearly forbids it. This may be a sufficient
reason not to do it.

--

Valentin Bonnard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Neal Becker <nbecker@fred.net>
Date: 1999/02/07
Raw View
The sum was just an example.  Consider any algorithm that must
preserve state information.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sirwillard@my-dejanews.com
Date: 1999/02/09
Raw View
In article <36BBB397.22AC@clipper.ens.fr>,
  Valentin Bonnard <bonnard@clipper.ens.fr> wrote:
> Christopher Eltschka wrote:
> >
> > James Kuyper wrote:
> > >
> > > sirwillard@my-dejanews.com wrote:
> >
> > [...]
> >
> > > > transform<z_type, y_type, Sum&>(zzz, yyy, x);
> > > >
> > > > This is more verbose, but should work.
>
> > The point is the last template argument: He explicitly gave a
> > _Reference_ for this argument, while the automatically chosen
> > specialisation takes just Sum, not Sum&.

Yes, indeed, this was the point I was trying to make.

> > An interesting idea. However, I'm not sure if this is guaranteed
> > to work by the standard, although I don't see a reason why it
> > shouldn't work. Anyone who knows what the standard says about
> > this?
>
> I don't see anything in the std which forbids it; OTOH,
> SGI STL clearly forbids it. This may be a sufficient
> reason not to do it.

Nothing that forbids it, but is there something that gaurantees it?  What
exactly in the SGI STL implementation (which isn't compliant any way) forbids
it?

It seemed the obvious answer to me, so I posted without researching, but now
I'm curious.  What exactly does the standard say on this?  Should the
"obvious" work on all implementations?

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/02/03
Raw View
On 03 Feb 99 05:22:05 GMT, Neal Becker <nbecker@fred.net> wrote:

>Then not x is passed, but a copy of x (using Sum (const Sum&)).  So
>the instance x isn't updated, and the value of the summation is not
>returned.
>
>Is there any reasonable fix?

Use std::accumulate.  Pay attention to the object/value returned by
this function.  Nearly everything in <algorithm> is pass and return
by value.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: hinnant@_anti-spam_lightlink.com (Howard Hinnant)
Date: 1999/02/03
Raw View
In article <87lnigi2di.fsf@nbecker.fred.net>, Neal Becker
<nbecker@fred.net> wrote:

> The functions such as transform won't work properly with an object
> that contains state.  For example, suppose you try to implement a
> sum.  Using a class sum with operator() and a member variable to
> maintain the state, the problem is that if you write:
>
> Sum x;
>
> transform (zzz, yyy, x)
>
> Then not x is passed, but a copy of x (using Sum (const Sum&)).  So
> the instance x isn't updated, and the value of the summation is not
> returned.
>
> Is there any reasonable fix?

How 'bout accumulate in <numeric>?  It also offers the ability to specify
the operation (still by value), but will return the value of the
summation:

template <class InputIterator, class T, class BinaryOperation>
T
accumulate(InputIterator first, InputIterator last, T init,
           BinaryOperation binary_op);

-Howard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/02/04
Raw View
sirwillard@my-dejanews.com wrote:
>
> In article <87lnigi2di.fsf@nbecker.fred.net>,
>   Neal Becker <nbecker@fred.net> wrote:
> > The functions such as transform won't work properly with an object
> > that contains state.  For example, suppose you try to implement a
> > sum.  Using a class sum with operator() and a member variable to
> > maintain the state, the problem is that if you write:
> >
> > Sum x;
> >
> > transform (zzz, yyy, x)
> >
> > Then not x is passed, but a copy of x (using Sum (const Sum&)).  So
> > the instance x isn't updated, and the value of the summation is not
> > returned.
> >
> > Is there any reasonable fix?
>
> My ignorance of the final standard may show here, but...
>
> The documentation with VC++ for transform doesn't mention a variant which
> takes three parameters.  Looking at your scarce code it's near impossible to

I'm sure he just left out the output iterator argument:

 transform(first, last, result, op);

> tell what you are trying to do.  In any event, transform defines all
> parameter types in the template.  All you should have to do is specify the
> types:
>
> transform<z_type, y_type, Sum&>(zzz, yyy, x);
>
> This is more verbose, but should work.

As you said, there are no 3-parameter versions of std::transform(). If
there were, the types you've specified would be unnecessary, because
they could be inferred from the types of the arguments.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/02/05
Raw View
James Kuyper wrote:
>
> sirwillard@my-dejanews.com wrote:

[...]

> > transform<z_type, y_type, Sum&>(zzz, yyy, x);
> >
> > This is more verbose, but should work.
>
> As you said, there are no 3-parameter versions of std::transform(). If
> there were, the types you've specified would be unnecessary, because
> they could be inferred from the types of the arguments.

The point is the last template argument: He explicitly gave a
_Reference_ for this argument, while the automatically chosen
specialisation takes just Sum, not Sum&.

An interesting idea. However, I'm not sure if this is guaranteed
to work by the standard, although I don't see a reason why it
shouldn't work. Anyone who knows what the standard says about
this?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Neal Becker <nbecker@fred.net>
Date: 1999/02/03
Raw View
The functions such as transform won't work properly with an object
that contains state.  For example, suppose you try to implement a
sum.  Using a class sum with operator() and a member variable to
maintain the state, the problem is that if you write:

Sum x;

transform (zzz, yyy, x)

Then not x is passed, but a copy of x (using Sum (const Sum&)).  So
the instance x isn't updated, and the value of the summation is not
returned.

Is there any reasonable fix?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/02/03
Raw View
Neal Becker wrote:
>
> The functions such as transform won't work properly with an object
> that contains state.  For example, suppose you try to implement a
> sum.  Using a class sum with operator() and a member variable to
> maintain the state, the problem is that if you write:
>
> Sum x;
>
> transform (zzz, yyy, x)
>
> Then not x is passed, but a copy of x (using Sum (const Sum&)).  So
> the instance x isn't updated, and the value of the summation is not
> returned.
>
> Is there any reasonable fix?

What about using accumulate?

Or using for_each, which returns the final object?
x=for_each(xxx, yyy, x);

transform is for making a transformation on the members of a
sequence, not for accumulating results. For example:

char rot13(char c); // does a rot13 on single char

std::string source, dest;
std::cin >> source; // read original
transform(source.begin(), source.end(), back_inserter(dest));
std::cout << dest; // write rot13 result
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: zdv176@zam368.zam.kfa-juelich.de (B.Mohr)
Date: 1999/02/03
Raw View
Neal Becker <nbecker@fred.net> writes:
>The functions such as transform won't work properly with an object
>that contains state.  For example, suppose you try to implement a
>sum.  Using a class sum with operator() and a member variable to
>maintain the state, the problem is that if you write:
>    Sum x;
>    transform (zzz, yyy, x)
>Then not x is passed, but a copy of x (using Sum (const Sum&)).  So
>the instance x isn't updated, and the value of the summation is not
>returned. Is there any reasonable fix?

Yes, use the STL algorithm "for_each" instead which returns the functor
after having it applied to every element

     Sum x;
     x = for_each(zzz, yyy, x);

Bernd
--
Bernd Mohr / Research Centre Juelich, ZAM, Germany / B.Mohr@fz-juelich.de


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sirwillard@my-dejanews.com
Date: 1999/02/03
Raw View
In article <87lnigi2di.fsf@nbecker.fred.net>,
  Neal Becker <nbecker@fred.net> wrote:
> The functions such as transform won't work properly with an object
> that contains state.  For example, suppose you try to implement a
> sum.  Using a class sum with operator() and a member variable to
> maintain the state, the problem is that if you write:
>
> Sum x;
>
> transform (zzz, yyy, x)
>
> Then not x is passed, but a copy of x (using Sum (const Sum&)).  So
> the instance x isn't updated, and the value of the summation is not
> returned.
>
> Is there any reasonable fix?

My ignorance of the final standard may show here, but...

The documentation with VC++ for transform doesn't mention a variant which
takes three parameters.  Looking at your scarce code it's near impossible to
tell what you are trying to do.  In any event, transform defines all
parameter types in the template.  All you should have to do is specify the
types:

transform<z_type, y_type, Sum&>(zzz, yyy, x);

This is more verbose, but should work.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]