473,756 Members | 8,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

portable and save reinterpret_cas t?

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_cas t. Is this a
portable solution that is guaranteed to work on all platforms?

template<typena me Key, typename T>
class foo
{
public:

std::pair<const Key, T>& at(std::size_t index)
{
return reinterpret_cas t<std::pair<con st Key, T>&>(pairs_.at( index));
}

void push_back(const std::pair<const Key, T>& pair)
{
pairs_.push_bac k(pair);
}

private:

std::vector<std ::pair<Key, T pairs_;
};

Thanks in advance,
Ralpe

Jan 4 '07 #1
12 1782

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_cas t. Is this a
portable solution that is guaranteed to work on all platforms?

template<typena me Key, typename T>
class foo
{
public:

std::pair<const Key, T>& at(std::size_t index)
{
return reinterpret_cas t<std::pair<con st Key, T>&>(pairs_.at( index));
}

void push_back(const std::pair<const Key, T>& pair)
{
pairs_.push_bac k(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_elsei nitalized with std::pair<somet hing, something_else>
value:

std::pair<const int, intTest()
{
std::pair<int, intp(1, 2); // This is only for illustration
return p;
}

Jan 4 '07 #2
Ondra Holub wrote:
I think that simpler solution is to return std::pair<const something,
something_elsei nitalized with std::pair<somet hing, 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).se cond = 4711;

Jan 4 '07 #3

ralpe napsal:
Ondra Holub wrote:
I think that simpler solution is to return std::pair<const something,
something_elsei nitalized with std::pair<somet hing, 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).se cond = 4711;
You're right, with references it is problem. I think that
reinterpret_cas t 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.

Jan 4 '07 #4
Ondra Holub wrote:
You're right, with references it is problem. I think that
reinterpret_cas t 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_cas t-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

Jan 4 '07 #5
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******** ************@v3 3g2000cwv.googl egroups.com...
Ondra Holub wrote:
>You're right, with references it is problem. I think that
reinterpret_ca st 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_cas t-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

Jan 4 '07 #6
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<val ue_typev;
v.push_back(val ue_type(1, 2)); // DOES NOT COMPILE

Thanks
Ralpe

Jan 4 '07 #7

"ralpe" <ra************ @gmx.netwrote in message
news:11******** **************@ 11g2000cwr.goog legroups.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<val ue_typev;
v.push_back(val ue_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

Jan 4 '07 #8
Right, well, in that case that's no option :)

- Sylvester

"ralpe" <ra************ @gmx.netwrote in message
news:11******** **************@ 11g2000cwr.goog legroups.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<val ue_typev;
v.push_back(val ue_type(1, 2)); // DOES NOT COMPILE

Thanks
Ralpe

Jan 4 '07 #9
"Sylvester Hesp" <s.****@oisyn.n lwrote in message
news:45******** *************@n ews.xs4all.nl.. .
Right, well, in that case that's no option :)

- Sylvester

"ralpe" <ra************ @gmx.netwrote in message
news:11******** **************@ 11g2000cwr.goog legroups.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<val ue_typev;
v.push_back(val ue_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
Jan 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
12858
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. To get around this limitation on double, I'd like to keep the bits of the double the *same* but change its interpretation to long. I can use "&" on longs. I tried to use reinterpret_cast for this purpose, but it returned zero every time. double...
3
2518
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 = reinterpret_cast<char*>(screen); for (int i=0; i<nsize; i++) {
1
1390
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 "workaround". The actual storage is allocated on the stack then inplace new constructs the object... please tell me if the TEmbed<> is legal/valid/portable/c++?
13
3108
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 reinterpret_cast is valid, according to the Standard.
2
2442
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 was to assign a value to an array of pointers. The selection of array depends upon a condition and the void* ptr has to be casted to the correct type and inserted in the array.
7
10911
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. Can I assume that reinterpret_cast is not safe and should not be used? Does it always succeed even if the cast is garbage?
32
2038
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
2
2962
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;
2
2681
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 optimizing. #include <iostream> int main() { long D1 = 0xbcfbc4f0d9b65179; long D2 = 0xbcfbc4f0d9b65042;
0
9384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9212
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9779
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8645
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6473
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5247
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.