| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"ralpe" |
| Date: |
04 Jan 2007 01:34:56 AM |
| Object: |
portable and save reinterpret_cast? |
Hi,
I have a template class foo which stores pairs of Keys and Ts in a
vector. Foo has an accessor function at(size_t) that returns a
reference to the pair at the specified index.
I do not want the users of foo to change the first value of a pair,
so I want to return a reference to a pair<const Key, T>, just like
std::map does. I cannot store pairs with a const first value in the
vector because such pairs would not be assignable.
My only solution so far is to use reinterpret_cast. Is this a
portable solution that is guaranteed to work on all platforms?
template<typename Key, typename T>
class foo
{
public:
std::pair<const Key, T>& at(std::size_t index)
{
return reinterpret_cast<std::pair<const Key, T>&>(pairs_.at(index));
}
void push_back(const std::pair<const Key, T>& pair)
{
pairs_.push_back(pair);
}
private:
std::vector<std::pair<Key, T> > pairs_;
};
Thanks in advance,
Ralpe
.
|
|
| User: "Ondra Holub" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 02:00:56 AM |
|
|
ralpe napsal:
Hi,
I have a template class foo which stores pairs of Keys and Ts in a
vector. Foo has an accessor function at(size_t) that returns a
reference to the pair at the specified index.
I do not want the users of foo to change the first value of a pair,
so I want to return a reference to a pair<const Key, T>, just like
std::map does. I cannot store pairs with a const first value in the
vector because such pairs would not be assignable.
My only solution so far is to use reinterpret_cast. Is this a
portable solution that is guaranteed to work on all platforms?
template<typename Key, typename T>
class foo
{
public:
std::pair<const Key, T>& at(std::size_t index)
{
return reinterpret_cast<std::pair<const Key, T>&>(pairs_.at(index));
}
void push_back(const std::pair<const Key, T>& pair)
{
pairs_.push_back(pair);
}
private:
std::vector<std::pair<Key, T> > pairs_;
};
Thanks in advance,
Ralpe
I think that simpler solution is to return std::pair<const something,
something_else> initalized with std::pair<something, something_else>
value:
std::pair<const int, int> Test()
{
std::pair<int, int> p(1, 2); // This is only for illustration
return p;
}
.
|
|
|
| User: "ralpe" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 02:08:12 AM |
|
|
Ondra Holub wrote:
I think that simpler solution is to return std::pair<const something,
something_else> initalized with std::pair<something, something_else>
value:
std::pair<const int, int> Test()
{
std::pair<int, int> p(1, 2); // This is only for illustration
return p;
}
No, that is no a solution because I need to return a reference to make
this possible:
myFoo.at(42).second = 4711;
.
|
|
|
| User: "Ondra Holub" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 02:34:21 AM |
|
|
ralpe napsal:
Ondra Holub wrote:
I think that simpler solution is to return std::pair<const something,
something_else> initalized with std::pair<something, something_else>
value:
std::pair<const int, int> Test()
{
std::pair<int, int> p(1, 2); // This is only for illustration
return p;
}
No, that is no a solution because I need to return a reference to make
this possible:
myFoo.at(42).second = 4711;
You're right, with references it is problem. I think that
reinterpret_cast should work everywhere for this case, becasue both
types differ only in const, so they should occupy exactly the same
place with the same alignment. I do not think, that const could lead to
some placement or alignment optimizations, but I am not sure.
Maybe 100% safe solution is to change API and do not return pair.
.
|
|
|
| User: "ralpe" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 03:09:55 AM |
|
|
Ondra Holub wrote:
You're right, with references it is problem. I think that
reinterpret_cast should work everywhere for this case, becasue both
types differ only in const, so they should occupy exactly the same
place with the same alignment. I do not think, that const could lead to
some placement or alignment optimizations, but I am not sure.
The reinterpret_cast-solution seems to work with Microsoft C++.
I think I'm going to use this solution if it will work with gcc too.
Maybe 100% safe solution is to change API and do not return pair.
Change of API is not a solution either. I need to write a class that
has the same API as std::map and is implemented in terms of a sorted
array/vector. So the class won't really have a at()-member function but
it will have iterators that return references to pair<const Key, T>.
Thanks,
Ralpe
.
|
|
|
| User: "Sylvester Hesp" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 05:25:32 AM |
|
|
Of course, if someone choses to specialize either (but not both)
std::pair<K, V> or std::pair<const K, V> to do something totally different
(for example, to swap the order of first and second in memory) you're
screwed. The question is whether this is important to you, but it is of
course perfectly legal C++ _and_ compatible with std::map.
Wouldn't it be a better solution if you let your map implementation
internally use a pair<const K, V> as well and const_cast away the const on
p.first whenever you need to change it? Then you won't have the issue of
pair<const K, V> and pair<K, V> not having the same memory layout.
- Sylvester
"ralpe" <ralph.peterson@gmx.net> wrote in message
news:1167901795.309217.3240@v33g2000cwv.googlegroups.com...
Ondra Holub wrote:
You're right, with references it is problem. I think that
reinterpret_cast should work everywhere for this case, becasue both
types differ only in const, so they should occupy exactly the same
place with the same alignment. I do not think, that const could lead to
some placement or alignment optimizations, but I am not sure.
The reinterpret_cast-solution seems to work with Microsoft C++.
I think I'm going to use this solution if it will work with gcc too.
Maybe 100% safe solution is to change API and do not return pair.
Change of API is not a solution either. I need to write a class that
has the same API as std::map and is implemented in terms of a sorted
array/vector. So the class won't really have a at()-member function but
it will have iterators that return references to pair<const Key, T>.
Thanks,
Ralpe
.
|
|
|
| User: "ralpe" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 06:15:05 AM |
|
|
Sylvester Hesp wrote:
Wouldn't it be a better solution if you let your map implementation
internally use a pair<const K, V> as well and const_cast away the const on
p.first whenever you need to change it? Then you won't have the issue of
pair<const K, V> and pair<K, V> not having the same memory layout.
- Sylvester
I tried to use pair<const K, V> internally, but didn't know how to do
it:
typedef std::pair<const int, int> value_type;
std::vector<value_type> v;
v.push_back(value_type(1, 2)); // DOES NOT COMPILE
Thanks
Ralpe
.
|
|
|
| User: "Jim Langston" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 07:36:18 AM |
|
|
"ralpe" <ralph.peterson@gmx.net> wrote in message
news:1167912905.552590.131730@11g2000cwr.googlegroups.com...
Sylvester Hesp wrote:
Wouldn't it be a better solution if you let your map implementation
internally use a pair<const K, V> as well and const_cast away the const
on
p.first whenever you need to change it? Then you won't have the issue of
pair<const K, V> and pair<K, V> not having the same memory layout.
- Sylvester
I tried to use pair<const K, V> internally, but didn't know how to do
it:
typedef std::pair<const int, int> value_type;
std::vector<value_type> v;
v.push_back(value_type(1, 2)); // DOES NOT COMPILE
it would be:
v.push_back( std::make_pair<const K, V>( 1, 2 ) );
but it doesn't seem to like the const either.
Thanks
Ralpe
.
|
|
|
|
| User: "Sylvester Hesp" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 08:27:13 AM |
|
|
Right, well, in that case that's no option :)
- Sylvester
"ralpe" <ralph.peterson@gmx.net> wrote in message
news:1167912905.552590.131730@11g2000cwr.googlegroups.com...
Sylvester Hesp wrote:
Wouldn't it be a better solution if you let your map implementation
internally use a pair<const K, V> as well and const_cast away the const
on
p.first whenever you need to change it? Then you won't have the issue of
pair<const K, V> and pair<K, V> not having the same memory layout.
- Sylvester
I tried to use pair<const K, V> internally, but didn't know how to do
it:
typedef std::pair<const int, int> value_type;
std::vector<value_type> v;
v.push_back(value_type(1, 2)); // DOES NOT COMPILE
Thanks
Ralpe
.
|
|
|
| User: "Sylvester Hesp" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 08:44:24 AM |
|
|
"Sylvester Hesp" <s.hesp@oisyn.nl> wrote in message
news:459d0f1e$0$330$e4fe514c@news.xs4all.nl...
Right, well, in that case that's no option :)
- Sylvester
"ralpe" <ralph.peterson@gmx.net> wrote in message
news:1167912905.552590.131730@11g2000cwr.googlegroups.com...
Sylvester Hesp wrote:
Wouldn't it be a better solution if you let your map implementation
internally use a pair<const K, V> as well and const_cast away the const
on
p.first whenever you need to change it? Then you won't have the issue of
pair<const K, V> and pair<K, V> not having the same memory layout.
- Sylvester
I tried to use pair<const K, V> internally, but didn't know how to do
it:
typedef std::pair<const int, int> value_type;
std::vector<value_type> v;
v.push_back(value_type(1, 2)); // DOES NOT COMPILE
Thanks
Ralpe
Btw my sincere apoligies for top-posting, I just figured out that's
considered bad posting style. I'll bottom-post from now on ;)
- Sylvester
.
|
|
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 12:13:10 PM |
|
|
ralpe wrote:
Sylvester Hesp wrote:
Wouldn't it be a better solution if you let your map implementation
internally use a pair<const K, V> as well and const_cast away the const
on p.first whenever you need to change it? Then you won't have the issue
of pair<const K, V> and pair<K, V> not having the same memory layout.
- Sylvester
I tried to use pair<const K, V> internally, but didn't know how to do
it:
typedef std::pair<const int, int> value_type;
std::vector<value_type> v;
v.push_back(value_type(1, 2)); // DOES NOT COMPILE
That doesn't work because vector elements must be assignable.
.
|
|
|
| User: "jim_taylor" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 01:31:02 PM |
|
|
Rolf Magnus wrote:
ralpe wrote:
That doesn't work because vector elements must be assignable.
How about you write your own templated sub-class that behaves like
std::pair but is actually a proxy to the real data - it takes two
members called first and second, but both are references to the real
first and second returned by vector::at().
template <class L,class R> class myv:public vector<pair<L,R> >
{
public:
class value_type
{
public:
typedef const l First;
typedef r Second;
First & first ;
Second & second;
value_type(First&first,Second&second): first(first),second(second) {}
value_type(pair<L,R>&pair): first(pair.first),second(pair.second) {}
operator pair<L,R>() {return pair<L,R>(first,second);}
};
value_type at(size_t i) {return vector::at(i);}
value_type operator[](size_t i) {return vector::at(i);}
};
.
|
|
|
|
|
|
|
|
|
|
|
| User: "Dizzy" |
|
| Title: Re: portable and save reinterpret_cast? |
04 Jan 2007 10:08:45 AM |
|
|
ralpe wrote:
Hi,
Hi
<snip>
template<typename Key, typename T>
class foo
{
public:
std::pair<const Key, T>& at(std::size_t index)
{
return reinterpret_cast<std::pair<const Key, T>&>(pairs_.at(index));
}
void push_back(const std::pair<const Key, T>& pair)
{
pairs_.push_back(pair);
}
private:
std::vector<std::pair<Key, T> > pairs_;
};
You said you need to change the fields in the pair but not the pair itself.
Thus you don't need to return a reference to the pair but a pair of
references. Thus try something like
std::pair<const Key&, T&> at(std::size_t index)
{
return std::pair<const Key&, T&>(pairs_.at(index).first,
pairs_.at(index).second);
}
Now that exact code probably doesnt work using std::pair constructor because
it asks for const T1& arguments thus forming a "reference to reference"
(and getting an error about that) but in that case just make your own class
just for that return of that function, its very easily for what you need,
something like:
template<typename T1, typename T2>
struct my_pair
{
my_pair()
:first(), second() {}
my_pair(T1 arg1, T2 arg2)
:first(arg1), second(arg2) {}
~my_pair() throw() {}
T1 first;
T2 second;
};
(and it can probably be improved, didn't tested the code but you get the
idea)
Hope this helps :)
--
Dizzy
http://dizzy.roedu.net
.
|
|
|
|

|
Related Articles |
|
|