On Jul 4, 8:58*am, Juha Nieminen <nos...@thanks.invalid> wrote:
> Ian Collins <ian-n...@hotmail.com> wrote:
> > On 07/ 4/10 06:23 AM, z65536 wrote:
> >> might be a dumb question of this, but do we use the form
> >> vector<T, allocator> *more often than the other? Under what
> >> circumstances do we use it?
>
> > You only provide an allocator if the vector needs one, which in my
> > experience is seldom.
>
> * If you are writing templated code which takes a container where
> the element type is explicitly specified, then you have to specify
> the allocator type as well. For example:
>
> template<typename Value_t>
> class SomeClass
> {
> *public:
> * * template<template<typename, typename> Container_t,
> typename Alloc_t>
> * * void someFunction(const Container_t<Value_t, Alloc_t>&
> container);
>
> };
>
> * You could omit the Alloc_t parts completely, and this would work
> with some older compilers (IIRC it would work with gcc 4.0 but not
> in 4.2), but I'm not exactly sure how standard that would be.
> Anybody has info on this?
Josuttis and Vandevoorde (C++ Templates, The Complete Guide) refer to
this in §13.5 `Relaxed Matching of Template Template Parameters',
where the following example is given:
#include <list>
// declares:
// namespace std {
// template <typename T,
// typename Allocator = allocator<T> >
// class list;
// }
template<typename T1,
typename T2,
template<typename> class Container>
// Container expects templates with only one parameter
class Relation {
public:
...
private:
Container<T1> dom1;
Container<T2> dom2;
};
int main()
{
Relation<int, double, std::list> rel;
// ERROR: std::list has more than one template parameter
...
}
They go on to say, in relation to the example:
"However, because std::list has a default template argument for its
allocator parameter, it would be possible to specify that Container
matches std::list and that each instantiation of Container uses the
default template argument of std::list."
They add, after having evidently provided a counter to one argument
for *not* permitting this kind of relaxed matching - "that the same
rule applies to matching function types." - that "Some C++ compilers
already offer the relaxed matching rule as an extension." This was
clearly the case with earlier versions of GCC, as you say. (In my
collection, gcc-3.4.4 accepts the code whilst gcc-4.4.1 fails it.)
They finish the section with:
"This issue has been brought up before the C++ standardization
committee, which is currently not inclined to add the relaxed
matching rule."
On this last point, see the C++ Standard Core Language Closed Issue
150 at
http://www.open-std.org/jtc1/sc22/wg...n1343.html#150
for details on the Committee's decision.
Regards
Paul Bibbings