portable and save reinterpret_cast?

R

ralpe

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
 
O

Ondra Holub

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;
}
 
R

ralpe

Ondra said:
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;
 
O

Ondra Holub

ralpe napsal:
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.
 
R

ralpe

Ondra said:
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
 
S

Sylvester Hesp

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
 
R

ralpe

Sylvester said:
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
 
J

Jim Langston

ralpe said:
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:
 
S

Sylvester Hesp

Sylvester Hesp said:
Right, well, in that case that's no option :)

- Sylvester

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
 
D

Dizzy

ralpe said:

Hi

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 :)
 
R

Rolf Magnus

ralpe said:
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.
 
J

jim_taylor

Rolf said:
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);}
};
 

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top