How to use vector<T>::reference?

C

Chris Dams

Dear all,

The following is accepted by the compiler

template <class T> class test
{ public:
typedef vector<int>::reference rt;
};

but after the change int -> T, obtaining the code fragment,

template <class T> class test
{ public:
typedef vector<T>::reference rt;
};

the compiler (g++ 4.0.2) says

test.C:8: error: type std::vector<T, std::allocator<_CharT> > is not derived from type test<T>
test.C:8: error: expected ; before rt

Can anyone explain why this happens and how I actually can use
vector<T>::reference in my class? I need it because T can also be bool and
vector<bool> has a different implementation.

Many thanks,
Chris
 
M

mlimber

Chris said:
Dear all,

The following is accepted by the compiler

template <class T> class test
{ public:
typedef vector<int>::reference rt;
};

but after the change int -> T, obtaining the code fragment,

template <class T> class test
{ public:
typedef vector<T>::reference rt;

Make that:

typedef typename vector<T>::reference rt;

For why, see
http://www.comeaucomputing.com/techtalk/templates/#typename .
};

the compiler (g++ 4.0.2) says

test.C:8: error: type std::vector<T, std::allocator<_CharT> > is not derived from type test<T>
test.C:8: error: expected ; before rt

Can anyone explain why this happens and how I actually can use
vector<T>::reference in my class? I need it because T can also be bool and
vector<bool> has a different implementation.

Cheers! --M
 
K

Kai-Uwe Bux

Chris said:
Dear all,

The following is accepted by the compiler

template <class T> class test
{ public:
typedef vector<int>::reference rt;
};

but after the change int -> T, obtaining the code fragment,

template <class T> class test
{ public:
typedef vector<T>::reference rt;

try:

typedef typename vector said:

[snip]


Best

Kai-Uwe Bux
 
R

Ron Natalie

Chris said:
template <class T> class test
{ public:
typedef vector<T>::reference rt;
};
vector<T>::reference is assumed not to be a type here.
The language requires types that are dependent on the
template variable to be called out with "typename"

typedef typename vector<T>::reference rt;
 
M

Michael

Chris said:
Dear all,

The following is accepted by the compiler

template <class T> class test
{ public:
typedef vector<int>::reference rt;
};

but after the change int -> T, obtaining the code fragment,

template <class T> class test
{ public:
typedef vector<T>::reference rt;
};

the compiler (g++ 4.0.2) says

test.C:8: error: type std::vector<T, std::allocator<_CharT> > is not derived from type test<T>
test.C:8: error: expected ; before rt

Can anyone explain why this happens and how I actually can use
vector<T>::reference in my class? I need it because T can also be bool and
vector<bool> has a different implementation.

Many thanks,
Chris

Could you please explain how to understand
vector<T>::reference? What's that?

Thanks,
Michael
 
C

Chris Dams

typedef typename vector<T>::reference rt;

Thanks to the people who have given the answer! This indeed solves it.
 
J

John Carson

Michael said:
Could you please explain how to understand
vector<T>::reference? What's that?

It is a typedef. Standard containers incorporate (at least) two typedefs:
value_type and reference. value_type is a typedef for the type of the
objects stored in the container (i.e., T), while reference is a typedef for
references to the type of objects stored in the container (i.e., T&). You
can use these typedefs even without using the containers, as illustrated
below, but the main purpose is of course to facilitate template programming
with the containers.

int main()
{
int x = 5;
double y = 7.6;

std::vector<int>::reference ri = x;
std::vector<double>::reference rd = y;

cout << ri << endl;
cout << rd << endl;
return 0;
}
 
N

Noah Roberts

John said:
It is a typedef. Standard containers incorporate (at least) two typedefs:
value_type and reference. value_type is a typedef for the type of the
objects stored in the container (i.e., T), while reference is a typedef for
references to the type of objects stored in the container (i.e., T&).

This may seem rather trivial as it seems obvious that value type should
always be T and reference should always be T&. However, either one
could actually be a "proxy" class that acts like you would expect a T
or T& to act but are not actually those things. You should always use
these typedefs when you are creating your own custom container through
composition as the OP has done.
 
K

Kai-Uwe Bux

Noah said:
This may seem rather trivial as it seems obvious that value type should
always be T and reference should always be T&. However, either one
could actually be a "proxy" class that acts like you would expect a T
or T& to act but are not actually those things.

Not correct for standard containers. The standard states:

std::vector<T>::value_type is always T.

std::vector<T,Allocator>::reference is Allocator::reference unless T=bool.

and for std::deque and std::list the second statement holds even for T=bool.

Also, std::allocator<T>::reference is T&.


There also is a good reason for these rigid requirements: suppose the client
of the standard library uses a type T and has specialized a template for
that type. You clearly would want the specialization to be chosen for the
You should always use
these typedefs when you are creating your own custom container through
composition as the OP has done.

Good practice anyway.


Best

Kai-Uwe Bux
 

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

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top