Hi,

This snippet is from the std::vector class:

template<class _Ty, class _A = allocator<_Ty> >
class vector {
public:
typedef vector<_Ty, _A> _Myt;
typedef _A allocator_type;
typedef _A::size_type size_type;
typedef _A::difference_type difference_type;
typedef _A::pointer _Tptr;
typedef _A::const_pointer _Ctptr;
typedef _A::reference reference;
typedef _A::const_reference const_reference;
typedef _A::value_type value_type;
typedef _Tptr iterator;
typedef _Ctptr const_iterator;
typedef reverse_iterator<const_iterator, value_type,
const_reference, _Ctptr, difference_type>
const_reverse_iterator;
typedef reverse_iterator<iterator, value_type,
reference, _Tptr, difference_type>
reverse_iterator;
explicit vector(const _A& _Al = _A())
: allocator(_Al), _First(0), _Last(0), _End(0) {}
explicit vector(size_type _N, const _Ty& _V = _Ty(),
.....
....
};

From this:
1) Does this imply that the 'allocator' class should have members/methods
called 'size_type', 'difference_type', 'value_type', etc. ?
What if someone deliberately passes in some class for _A (allocator),
which does not have 'size_type', etc? Will the compiler complain?

2) When are contructors made explicit?

I've already looked through some book on this, but it wasn't clear.

TIA,
SD

Re: Use of 'typename' by John

John
Thu Apr 21 02:28:10 CDT 2005

"SD" <SD@discussions.microsoft.com> wrote in message
news:62191EAC-06BD-43AE-85F5-0B0A8A220E12@microsoft.com


It is unclear what your question has to do with use of typename.

> Hi,
>
> This snippet is from the std::vector class:
>
> template<class _Ty, class _A = allocator<_Ty> >
> class vector {
> public:
> typedef vector<_Ty, _A> _Myt;
> typedef _A allocator_type;
> typedef _A::size_type size_type;
> typedef _A::difference_type difference_type;
> typedef _A::pointer _Tptr;
> typedef _A::const_pointer _Ctptr;
> typedef _A::reference reference;
> typedef _A::const_reference const_reference;
> typedef _A::value_type value_type;
> typedef _Tptr iterator;
> typedef _Ctptr const_iterator;
> typedef reverse_iterator<const_iterator, value_type,
> const_reference, _Ctptr, difference_type>
> const_reverse_iterator;
> typedef reverse_iterator<iterator, value_type,
> reference, _Tptr, difference_type>
> reverse_iterator;
> explicit vector(const _A& _Al = _A())
>> allocator(_Al), _First(0), _Last(0), _End(0) {}
> explicit vector(size_type _N, const _Ty& _V = _Ty(),
> .....
> ....
> };
>
> From this:
> 1) Does this imply that the 'allocator' class should have
> members/methods called 'size_type', 'difference_type',
> 'value_type', etc. ?

Yes.

> What if someone deliberately passes in some
> class for _A (allocator), which does not have 'size_type', etc?
> Will the compiler complain?

Try it and see. (The answer is yes.)

> 2) When are contructors made explicit?

When the code author wants to prevent automatic conversions from another
type to the type in question. To illustrate, suppose that a function has a
parameter of

const vector<int> &

Then, in the absence of the explicit keyword, if you were to pass an integer
as an argument to the function, the vector constructor that takes an int as
an argument would be called to create a vector temporary object for the
function to use. This is fine if that is what the person calling the
function intended, but it is at least as likely that they made a mistake and
supplied an integer argument when they meant to supply a vector.

With the explicit keyword, the constructor must be called explicitly in
order to create temporaries and hence the passing of the integer in place of
the vector will be flagged as an error by the compiler and the mistake
discovered.


--
John Carson


Re: Use of 'typename' by SD

SD
Thu Apr 21 03:07:01 CDT 2005

Thanks John. I'd left out the question on typename.

1) In the code below:

template<class C> typename C::iterator find_last(C& v, typename
C::value_type v)
{
typename C::reverse_iterator ri = find(c.rbegin(), c.rend(), v);
if (ri == c.rend()) return c.end();
typename C::iterator i = ri.base();
return --i;
}

why is the keyword 'typename' required in "typename C::iterator", or
in "typename C::reverse_iterator" ? Why not just use "C::iterator" or
"C::reverse_iterator" instead?

2) Could you point me to some material which explains the basic concepts of
allocators.

Re: Use of 'typename' by Tom

Tom
Thu Apr 21 04:39:02 CDT 2005

SD wrote:
> Thanks John. I'd left out the question on typename.
>
> 1) In the code below:
>
> template<class C> typename C::iterator find_last(C& v, typename
> C::value_type v)
> {
> typename C::reverse_iterator ri = find(c.rbegin(), c.rend(), v);
> if (ri == c.rend()) return c.end();
> typename C::iterator i = ri.base();
> return --i;
> }
>
> why is the keyword 'typename' required in "typename C::iterator", or
> in "typename C::reverse_iterator" ? Why not just use "C::iterator" or
> "C::reverse_iterator" instead?

The C++ standard requires that "typename" is used for member typedefs of
classes whose exact type depends on a template argument. This allows the
compiler to disambiguate such typedefs from member variables. e.g.

T::i* p; //(T is a template parameter, say)
Is that declaring a variable, p, of type pointer to T::i? Or is it
multiplying the static member T::i by p? "typename T::i* p;" makes it
the former, without the typename it is the latter.

> 2) Could you point me to some material which explains the basic concepts of
> allocators.

Google turned these up:
http://www.langer.camelot.de/Articles/C++Report/Allocators/Allocators.html
http://www.cuj.com/documents/s=7988/cujcexp1912austern/
Angelika Langer and Matt Austern are a well known C++ gurus.

Tom

Re: Use of 'typename' by John

John
Thu Apr 21 05:05:14 CDT 2005

"SD" <SD@discussions.microsoft.com> wrote in message
news:BF7AFF5D-54E0-45DD-9E53-941C91619AF5@microsoft.com
> Thanks John. I'd left out the question on typename.
>
> 1) In the code below:
>
> template<class C> typename C::iterator find_last(C& v, typename
> C::value_type v)
> {
> typename C::reverse_iterator ri = find(c.rbegin(), c.rend(), v);
> if (ri == c.rend()) return c.end();
> typename C::iterator i = ri.base();
> return --i;
> }
>
> why is the keyword 'typename' required in "typename C::iterator", or
> in "typename C::reverse_iterator" ? Why not just use "C::iterator" or
> "C::reverse_iterator" instead?

http://groups-beta.google.com/group/comp.lang.c++/browse_frm/thread/e3f485bbeefe34a5/40ca3a37f8076c3c?q=John+Carson+typename&rnum=2#40ca3a37f8076c3c

> 2) Could you point me to some material which explains the basic
> concepts of allocators.

Besides Tom's references, you might consider getting Nicolai Josuttis, The
C++ Standard Library, which has a chapter on allocators and is the standard
(small "s") text on the C++ library.


--
John Carson


Re: Use of 'typename' by Michael

Michael
Thu Apr 21 14:43:37 CDT 2005

"SD" <SD@discussions.microsoft.com> wrote in message
news:BF7AFF5D-54E0-45DD-9E53-941C91619AF5@microsoft.com...
< snip>
> 2) Could you point me to some material which explains the basic concepts
of
> allocators.

Also see this article by Gabriel Fleseriu and Andreas Masur: "Allocators
(STL)" at http://www.codeguru.com/Cpp/Cpp/cpp_mfc/stl/article.php/c4079

It explains how to write your own custom allocator, with examples.

Mike