vector<T> v.s. vector<T,allocator>

Z

z65536

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?

I saw some code using it any way, but in some cases, such a statement
simply wont compile.

Thanks for the kind answers,
Hai
 
I

Ian Collins

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.
I saw some code using it any way, but in some cases, such a statement
simply wont compile.

Such as?
 
J

Juha Nieminen

Ian Collins said:
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?
 
P

Paul Bibbings

  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/wg21/docs/papers/2001/n1343.html#150
for details on the Committee's decision.

Regards

Paul Bibbings
 
R

red floyd

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?

I saw some code using it any way, but in some cases, such a statement
simply wont compile.

Technically, you are specifying an allocator. Per 23.2.4/2, the
actual definition of vector is:

namespace std {
template <class T, class Allocator = allocator<T> >
class vector {
// body redacted
};
}

So when you declare a variable of type vector<T>, you're actually
declaring something of type
vector<T, allocator<T> >. (std:: redacted).

In general you only explicitly specify the allocator when you need a
custom allocator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top