Topic: STL containers
Author: James Kuyper <kuyper@wizard.net>
Date: 1997/11/04 Raw View
I think I've finally figured out what it means for a template parameter
to be itself a template. This allows me to ask directly a question that
I asked indirectly a few days ago:
The STL containers are typically defined as:
template<typename T, class Allocator=allocator<T>>
class container{ /* details */ };
As I explained in my previous question, container<T,A> seems to violate
the standard's container requirements if A::value_type is not the same
as T. Why weren't the containers defined as:
template<typename T, template<class> class Allocator=allocator>
class container{ /* details */ };
The best reason I can think of is that the STL containers are older than
the changes that permit the second style of definition. Are there any
other reasons?
I also asked if container<T,A> could be made to work properly as long as
T and A::value_type were related in some way. I think that I can now
answer my own question: if the objects actually stored in the container
were of the type
union contents{
T t;
A::value_type v;
};
then it could be written to work correctly so long as T is derived from
A::value_type, or vice versa. However, I don't see any use for this
idea, and unions like this worry me.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1997/11/04 Raw View
James Kuyper <kuyper@wizard.net> writes:
> I think I've finally figured out what it means for a template parameter
> to be itself a template. This allows me to ask directly a question that
> I asked indirectly a few days ago:
>
> The STL containers are typically defined as:
>
> template<typename T, class Allocator=allocator<T>>
> class container{ /* details */ };
>
> As I explained in my previous question, container<T,A> seems to violate
> the standard's container requirements if A::value_type is not the same
> as T. Why weren't the containers defined as:
>
> template<typename T, template<class> class Allocator=allocator>
> class container{ /* details */ };
>
They originally were. If you look at the original documentation for
the HP STL (A. Stepanov and M. Lee, "The Standard Template Library",
HPL-95-11 (R.1), available by anonymous FTP from butler.hpl.hp.com),
you'll see that containers were defined just that way. At that time,
allocators didn't have the tricky rebind mechanism.
Allocators have been changed several times during the standardization
process.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/11/05 Raw View
James Kuyper <kuyper@wizard.net> writes:
> The STL containers are typically defined as:
>
> template<typename T, class Allocator=allocator<T>>
> class container{ /* details */ };
> Why weren't the containers defined as:
>
> template<typename T, template<class> class Allocator=allocator>
> class container{ /* details */ };
>
> The best reason I can think of is that the STL containers are older than
> the changes that permit the second style of definition. Are there any
> other reasons?
One method uses template member rebind, the other template
template parameters; they are equivalent in this example,
but the later has never been implemented on a compiler.
There was even a proposal to remove template template
parameters during the last meeting.
Typically, a template class takes as argument a class C<T>,
but may actually need any instantiation of it (a containner
may use allocator<anything_it_wants>). So you don't actually
pass a class, but a class template:
template <class T>
class allocator
{
template <class U>
class rebind { typedef allocator<U> allocator; ... };
...
};
template <typename T, class Allocator=allocator<T>>
class container{
typdef Allocator::rebind<int>::allocator my_alloc;
...
And the STL is older than current allocators and member
templates.
> I also asked if container<T,A> could be made to work properly
> as long as T and A::value_type were related in some way.
This will be clarified during the next meeting; the
answer should be no.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]