help for casting

A

abir

Hi,
I have one specific purpose container, where it stores struct elem in
contiguous memory as,
elem* buffer_;

the struct elem is
struct elem{
size_type index;
value_type value;
key_type key;
};
where types are of usual meaning.
i am interested to have one operator value_type& operator[](size_type
i), rather than the usual
elem& operator[](size_type i);
can anyone give a direction how i can cast buffer_ to value_type in
portable way? i.e irrespective of alignments etc.
or if i make value_type value as the first in order, will it work ?

thanks
abir
 
J

Jim Langston

abir said:
Hi,
I have one specific purpose container, where it stores struct elem in
contiguous memory as,
elem* buffer_;

the struct elem is
struct elem{
size_type index;
value_type value;
key_type key;
};
where types are of usual meaning.
i am interested to have one operator value_type& operator[](size_type
i), rather than the usual
elem& operator[](size_type i);
can anyone give a direction how i can cast buffer_ to value_type in
portable way? i.e irrespective of alignments etc.
or if i make value_type value as the first in order, will it work ?


I'm not sure I understand your difficulty. Why don't you just declare
value_type& operator[]( size_type i) for your container and have it return
the correct reference. What difficulty are you coming across?
 
E

Erik Wikström

Hi,
I have one specific purpose container, where it stores struct elem in
contiguous memory as,
elem* buffer_;

the struct elem is
struct elem{
size_type index;
value_type value;
key_type key;
};
where types are of usual meaning.
i am interested to have one operator value_type& operator[](size_type
i), rather than the usual
elem& operator[](size_type i);
can anyone give a direction how i can cast buffer_ to value_type in
portable way? i.e irrespective of alignments etc.
or if i make value_type value as the first in order, will it work ?


value_type& operator[](size_type i)
{
return buffer_.value;
}
 

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

No members online now.

Forum statistics

Threads
473,800
Messages
2,569,657
Members
45,417
Latest member
BonitaNile
Top