468,491 Members | 2,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

How to use vector<T>::reference?

Dear all,

The following is accepted by the compiler

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

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

template <class Tclass 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<boolhas a different implementation.

Many thanks,
Chris
Aug 12 '06 #1
8 3818
Chris Dams wrote:
Dear all,

The following is accepted by the compiler

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

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

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

typedef typename vector<T>::reference rt;

For why, see
http://www.comeaucomputing.com/techt...ates/#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<boolhas a different implementation.
Cheers! --M

Aug 12 '06 #2
Chris Dams wrote:
>
template <class Tclass 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;

Aug 12 '06 #3
Chris Dams wrote:
Dear all,

The following is accepted by the compiler

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

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

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

typedef typename vector<T>::reference rt;
};
[snip]
Best

Kai-Uwe Bux

Aug 12 '06 #4

Chris Dams wrote:
Dear all,

The following is accepted by the compiler

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

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

template <class Tclass 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<boolhas a different implementation.

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

Thanks,
Michael

Aug 12 '06 #5
> typedef typename vector<T>::reference rt;

Thanks to the people who have given the answer! This indeed solves it.
Aug 12 '06 #6
"Michael" <mi*******@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com
Chris Dams wrote:
>Dear all,

The following is accepted by the compiler

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

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

template <class Tclass 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<Ttest.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<boolhas a different implementation.

Many thanks,
Chris

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;
}
--
John Carson
Aug 13 '06 #7

John Carson wrote:
"Michael" <mi*******@gmail.comwrote in message
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&).
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.

Aug 14 '06 #8
Noah Roberts wrote:
>
John Carson wrote:
>"Michael" <mi*******@gmail.comwrote in message
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&).

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
type std::vector<T>::value_type, as well.

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
Aug 14 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Stefan Höhne | last post: by
11 posts views Thread by Michael | last post: by
8 posts views Thread by Joseph Turian | last post: by
9 posts views Thread by richard_lavoie | last post: by
42 posts views Thread by barcaroller | last post: by
3 posts views Thread by Rune Allnor | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.