Topic: STL vector and container requirements


Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/03/28
Raw View
Scott Garland writes:

>    while reading the draft standard for STL, i noticed that vector's
> must provide container functionality (in particular <,>,<=, etc...);
> functionality that doesn't seem to be a requirement of the early
> versions of the STL.

In my reading, this is not a strong requirement from containers.  A
precondition for these operators to work is that < is defined for
values of the contained type.  If it is not, this operation cannot be
used, and it should not be instantiated by conforming compilers.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: sgarland@world.std.com (Scott Garland)
Date: 1997/03/25
Raw View
hello,
   while reading the draft standard for STL, i noticed that vector's
must provide container functionality (in particular <,>,<=, etc...);
functionality that doesn't seem to be a requirement of the early
versions of the STL.

  why is this a good idea? it seems to me that it encourages one
to do things like this:

class foo {
public:
  operator int () const { return 0; }  // so foo can be vector-ized
private:
   // stuff
};

vector<foo> foo_vec;

  is there some new class that provides a simple vector? without
having to define meaningless operators?

thanks,
scott
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Matt Austern <austern@mti.sgi.com>
Date: 1997/03/25
Raw View
sgarland@world.std.com (Scott Garland) writes:

>    while reading the draft standard for STL, i noticed that vector's
> must provide container functionality (in particular <,>,<=, etc...);
> functionality that doesn't seem to be a requirement of the early
> versions of the STL.

Actually, equality and comparison operators have been part of the
container requirements for a long time.  If you look at the original
technical report that gave the STL specifications (A. Stepanov and
M. Lee, "The Standard Template Library".  HP Technical Report
HPL-95-11 (R.1), 1995.), you'll see that vectors do provide operator==
and operator<.  Table 8 in that document, "Container Requirements",
specifies the semantics of those operators.

Note that objects contained in a vector do not have to provide
operator== and operator<.  Member functions of a class template are
only instantiated if they are used; this means that it's OK to have a
vector<T> for a class T that has no operator<, so long as you never
write anything like "v1 < v2".

(Unfortunately, some compilers get this wrong: some compilers
incorrectly try to instantiate all member functions of a class
template, regardless of whether they're used.  If you have a compiler
like that, then some parts of the standard library won't work very
well.  My only advice to you, in that case, is to complain to your
compiler vendor and ask them to fix that bug.)
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: jbuck@Synopsys.COM (Joe Buck)
Date: 1997/03/26
Raw View
sgarland@world.std.com writes:
>   while reading the draft standard for STL, i noticed that vector's
>must provide container functionality (in particular <,>,<=, etc...);
>functionality that doesn't seem to be a requirement of the early
>versions of the STL.

No, that was always there.  HP's vector has operator == and operator < .

>  why is this a good idea? it seems to me that it encourages one
>to do things like this:

>class foo {
>public:
>  operator int () const { return 0; }  // so foo can be vector-ized
>private:
>   // stuff
>};

You're assuming that foo must have operator == and operator <.  But
that's not so.  It's true that if a user attempts to use operator ==
and operator < on vector<foo>, then foo objects must be comparable,
but under the draft standard rules functions that aren't instantiated
need not be instantiatable.

Many current compilers (e.g. g++ 2.7.2) do attempt to expand all members
of a templated class, so for now you must have == and < on objects that
go into vectors.  But that's a restriction that is going away.

>  is there some new class that provides a simple vector? without
>having to define meaningless operators?

Yes, it's called vector.



--
-- Joe Buck http://www.synopsys.com/pubs/research/people/jbuck.html

Help stamp out Internet spam: see http://www.vix.com/spam/
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]