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 12 1739
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_elseinitalized with std::pair<something, something_else>
value:
std::pair<const int, intTest()
{
std::pair<int, intp(1, 2); // This is only for illustration
return p;
}
Ondra Holub wrote:
I think that simpler solution is to return std::pair<const something,
something_elseinitalized with std::pair<something, something_else>
value:
std::pair<const int, intTest()
{
std::pair<int, intp(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;
ralpe napsal:
Ondra Holub wrote:
I think that simpler solution is to return std::pair<const something,
something_elseinitalized with std::pair<something, something_else>
value:
std::pair<const int, intTest()
{
std::pair<int, intp(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.
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
Of course, if someone choses to specialize either (but not both)
std::pair<K, Vor std::pair<const K, Vto 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, Vas 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, Vand pair<K, Vnot having the same memory layout.
- Sylvester
"ralpe" <ra************@gmx.netwrote in message
news:11********************@v33g2000cwv.googlegrou ps.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
Sylvester Hesp wrote:
Wouldn't it be a better solution if you let your map implementation
internally use a pair<const K, Vas 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, Vand pair<K, Vnot having the same memory layout.
- Sylvester
I tried to use pair<const K, Vinternally, but didn't know how to do
it:
typedef std::pair<const int, intvalue_type;
std::vector<value_typev;
v.push_back(value_type(1, 2)); // DOES NOT COMPILE
Thanks
Ralpe
"ralpe" <ra************@gmx.netwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
Sylvester Hesp wrote:
>Wouldn't it be a better solution if you let your map implementation internally use a pair<const K, Vas 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, Vand pair<K, Vnot having the same memory layout.
- Sylvester
I tried to use pair<const K, Vinternally, but didn't know how to do
it:
typedef std::pair<const int, intvalue_type;
std::vector<value_typev;
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
Right, well, in that case that's no option :)
- Sylvester
"ralpe" <ra************@gmx.netwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
Sylvester Hesp wrote:
>Wouldn't it be a better solution if you let your map implementation internally use a pair<const K, Vas 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, Vand pair<K, Vnot having the same memory layout.
- Sylvester
I tried to use pair<const K, Vinternally, but didn't know how to do
it:
typedef std::pair<const int, intvalue_type;
std::vector<value_typev;
v.push_back(value_type(1, 2)); // DOES NOT COMPILE
Thanks
Ralpe
"Sylvester Hesp" <s.****@oisyn.nlwrote in message
news:45*********************@news.xs4all.nl...
Right, well, in that case that's no option :)
- Sylvester
"ralpe" <ra************@gmx.netwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
>Sylvester Hesp wrote:
>>Wouldn't it be a better solution if you let your map implementation internally use a pair<const K, Vas 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, Vand pair<K, Vnot having the same memory layout.
- Sylvester
I tried to use pair<const K, Vinternally, but didn't know how to do it:
typedef std::pair<const int, intvalue_type; std::vector<value_typev; 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
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
ralpe wrote:
Sylvester Hesp wrote:
>Wouldn't it be a better solution if you let your map implementation internally use a pair<const K, Vas 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, Vand pair<K, Vnot having the same memory layout.
- Sylvester
I tried to use pair<const K, Vinternally, but didn't know how to do
it:
typedef std::pair<const int, intvalue_type;
std::vector<value_typev;
v.push_back(value_type(1, 2)); // DOES NOT COMPILE
That doesn't work because vector elements must be assignable.
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 Rclass 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);}
}; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Suzanne Vogel |
last post by:
I'd like to convert a double to a binary representation. I can use the
"&" bit operation with a bit mask to convert *non* float types to binary
representations, but I can't use "&" on doubles.
...
|
by: pfemat |
last post by:
Hi there,
i am doing some heavy computations whose result i want to save
to harddisk. the results are in an long array named screen
and i thought of doing
char *tmp =...
|
by: DonBot |
last post by:
Hello Group,
I want to use this peace of code as
a core functionality for a polymorphic iterator.
Since i don't want to use plain new to create a
polymorphic iterator, i thought about this...
|
by: Jakob Bieling |
last post by:
Hi,
I am trying to determine the endianness of the system as follows:
int i = 1;
bool is_little = *reinterpret_cast <char*> (&i) != 0;
But now I was asking myself, if this use of...
|
by: Taran |
last post by:
Hi All,
I was trying some code which essentially does what the function 'func'
here does. To simplify the things and make a consise post I have
created a dummy app to show my problem.
The issue...
|
by: Peter |
last post by:
I never used reinterpret_cast -- probably because I don't know what it
means.
Can somebody enlighten me?
I looked into Stroustrup's "The annoted C++ reference manual" -- but
this was no help....
|
by: r.z. |
last post by:
class vector3
{
public:
union
{
float data;
struct
{
float x, y, z;
};
|
by: jasm |
last post by:
hello everybody!
I have used reinterpret_cast for interpret a class object as a char*.
This is the object:
template<class T>
class Coordinates {
public:
T *x;
T *y;
|
by: ciccio |
last post by:
Hi,
I was wondering what the main reason is why reinterpret_cast fails to work
as expected when using optimizations. Here is the simple example code which
fail to give the correct result when...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |