Re: BUG: "const _Ty&" vs "const_reference"

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 01/13/05


Date: Thu, 13 Jan 2005 14:31:50 -0600

Marc-Antoine Ruel wrote:

>In <list> and <vector>, "const _Ty&" is used everywhere instead of
>const_reference which is defined in the class allocator. There is around 9
>errors in vector and 15 in list. I haven't looked at the other containers.
>
>This causes that if a custom allocator redefine const_reference, this is
>simply not used and const _Ty& is used instead.
>
>I looked a bit around the net but couldn't find any information related to
>this.

Look in the C++ Standard, 20.1.5, Table 32. It's a hard requirement that for
allocator<T>, reference == T& and const_reference == const T&. Since
vector/list::reference/const_reference are defined per their respective
allocators, implementations are free to substitute at will.

In addition, 20.1.5 goes on to give containers permission to assume pointer,
const_pointer, size_type, and difference_type have their ordinary
definitions. Thus, it's purely a QoI issue whether or not containers respect
these typedefs defined by their allocators.

That said, suppose you have an implementation that supports, say, near and
far pointers. Doesn't it also need near and far references? Pre-standard STL
docs described allocator reference types merely as "lvalues of T", which
would seem to support this, but the standard gives the stricter definition
mentioned earlier, which would seem to rule it out.

-- 
Doug Harrison
Microsoft MVP - Visual C++


Relevant Pages

  • Re: Ada containers and custom allocators
    ... standard storage pool). ... containers manageable. ... course it is the container which would then instantiate the allocator ... Only if you put severe limits on the implementations. ...
    (comp.lang.ada)
  • Re: allocator requirements
    ... >> the allocator for vector at least. ... This seems to directly contradict ... >> several containers with different value types. ... from the standard it cannot have the correct value type for all these ...
    (comp.lang.cpp)
  • allocator question (reposted here... sorry for the mistake)
    ... when passing an allocator to SEVERAL containers at the same time, ... template ... class MyContainer ...
    (microsoft.public.vc.stl)
  • allocator question
    ... when passing an allocator to SEVERAL containers at the same time, ... template ... class MyContainer ...
    (microsoft.public.vc.language)
  • Re: operator= in allocators
    ... But that makes no logical sense if containers own their allocator. ... vectorfoo() ... SomeAllocator a; ...
    (comp.lang.cpp)