Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector<T> v.s. vector<T,allocator>

Reply
Thread Tools

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

 
 
z65536
Guest
Posts: n/a
 
      07-03-2010
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-03-2010
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.

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


Such as?

--
Ian Collins
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      07-04-2010
Ian Collins <ian-> 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?
 
Reply With Quote
 
Paul Bibbings
Guest
Posts: n/a
 
      07-04-2010
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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      07-06-2010
On Jul 3, 11:23*am, z65536 <yihai2...@gmail.com> 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?
>
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57