473,503 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>*

Hi,

Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?

Alex Vinokur
Oct 12 '08 #1
10 3742
On 2008-10-12 13:16, Alex Vinokur wrote:
Hi,

Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
const_cast might work, but I don't think this counts as casting away
constness, you should probably use reinterpret_cast.

--
Erik Wikström
Oct 12 '08 #2
On Oct 12, 7:16*pm, Alex Vinokur <ale...@users.sourceforge.netwrote:
Hi,

Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
const pair<const unsigned char*, size_t>* p1 = 0;
const pair<unsigned char*, size_t>* p2 =
reinterpret_cast<const pair<unsigned char*, size_t>*>(p1);

but reinterpre_cast should be avoided if possible.
*Practically*, I wonder you can just do it this way:

char* s = const_cast<char*>(p1->first);

Or can you tell me the scenario you are in?

--
Best Regards
Barry
Oct 12 '08 #3
On Oct 12, 2:22*pm, Barry <dhb2...@gmail.comwrote:
On Oct 12, 7:16*pm, Alex Vinokur <ale...@users.sourceforge.netwrote:
Hi,
Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?

* const pair<const unsigned char*, size_t>* p1 = 0;
* const pair<unsigned char*, size_t>* p2 =
* * * * reinterpret_cast<const pair<unsigned char*, size_t>*>(p1);

but reinterpre_cast should be avoided if possible.
*Practically*, I wonder you can just do it this way:

* char* s = const_cast<char*>(p1->first);

Or can you tell me the scenario you are in?
I have function foo1 (const pair<unsigned char*, size_t>* p);
I need also function foo2 (const pair<const unsigned char*, size_t>*
p) that does the same thing as foo1().

Currently
void foo2 (const pair<const unsigned char*, size_t>* p)
{
// I would like to use here C++-style casting
const pair<unsigned char*, size_t>* p1 = ( const pair<unsigned
char*, size_t>* ) p;
foo1(p1);
}

Alex Vinokur
Oct 12 '08 #4
Sam
Alex Vinokur writes:
Hi,

Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
Yes, if you write your own conversion function. Although, if you are
attempting to do something like this, then it's fairly likely that whatever
you're really trying to do, you're doing it the wrong way.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkjx9tEACgkQx9p3GYHlUOKbtQCdHLZUGkm+k5 aHKdQtEiQtcc3o
vaMAniIYUdDc0XEsfWITCcbqSe9GLsme
=goWJ
-----END PGP SIGNATURE-----

Oct 12 '08 #5
On Oct 12, 9:05*pm, Alex Vinokur <ale...@users.sourceforge.netwrote:
On Oct 12, 2:22*pm, Barry <dhb2...@gmail.comwrote:
On Oct 12, 7:16*pm, Alex Vinokur <ale...@users.sourceforge.netwrote:
Hi,
Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
* const pair<const unsigned char*, size_t>* p1 = 0;
* const pair<unsigned char*, size_t>* p2 =
* * * * reinterpret_cast<const pair<unsigned char*, size_t>*>(p1);
but reinterpre_cast should be avoided if possible.
*Practically*, I wonder you can just do it this way:
* char* s = const_cast<char*>(p1->first);
Or can you tell me the scenario you are in?

I have function foo1 (const pair<unsigned char*, size_t>* p);
I need also function foo2 (const pair<const unsigned char*, size_t>*
p) that does the same thing as foo1().

Currently
void foo2 (const pair<const unsigned char*, size_t>* p)
{
* // I would like to use here C++-style casting
* const pair<unsigned char*, size_t>* p1 = ( const pair<unsigned
char*, size_t>* ) p;
* foo1(p1);

}
I'm sorry that my previous post misled you.
I think sam got my answer.

What I asked is that I was confused that why you need
such conversion. And I was expecting to see if there's some
way to avoid such conversion.

If you just wanted to learn the language. OK, "reinterpret_cast"
as C++-style cast, or just use C-style cast. While in practice,
avoid doing this.

--
Best Regards
Barry

Oct 12 '08 #6
On 12 Okt., 13:16, Alex Vinokur <ale...@users.sourceforge.netwrote:
Hi,

Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?

Alex Vinokur
Only a reinterpret_cast works: the two types are unrelated. Why do you
want to do this? It would be better to fix your design instead.

/Peter
Oct 12 '08 #7
On Oct 12, 4:08 pm, peter koch <peter.koch.lar...@gmail.comwrote:
On 12 Okt., 13:16, Alex Vinokur <ale...@users.sourceforge.netwrote:
Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
Only a reinterpret_cast works: the two types are unrelated.
Why do you want to do this? It would be better to fix your
design instead.
Reinterpret_cast doesn't work. You can't do a reinterpret_cast
to or from a user defined type, and instantiations of std::pair
are considered user defined types.

What he can do is construct a new std::pair, e.g.:

std::pair< unsigned char*, size_t const p2
= std::make_pair(
const_cast< unsigned char* >( p1.first ),
p2.second ) ;

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 13 '08 #8
On Oct 12, 2:05*pm, Alex Vinokur <ale...@users.sourceforge.netwrote:
I have function foo1 (const pair<unsigned char*, size_t>* p);
I need also function foo2 (const pair<const unsigned char*, size_t>*
p) that does the same thing as foo1().
template <class T>
void foo_impl (const pair <T, size_t>* p) {...}

typedef pair <const unsigned char*, size_tpair1;
typedef pair <unsigned char*, size_tpair2;

void foo1 (const pair1* p) {foo_impl (p);}
void foo2 (const pair2* p) {foo_impl (p);}

Regards,
Vidar Hasfjord
Oct 13 '08 #9
On 2008-10-13 11:27, James Kanze wrote:
On Oct 12, 4:08 pm, peter koch <peter.koch.lar...@gmail.comwrote:
>On 12 Okt., 13:16, Alex Vinokur <ale...@users.sourceforge.netwrote:
Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
>Only a reinterpret_cast works: the two types are unrelated.
Why do you want to do this? It would be better to fix your
design instead.

Reinterpret_cast doesn't work. You can't do a reinterpret_cast
to or from a user defined type, and instantiations of std::pair
are considered user defined types.
He wanted to cast from a pointer to a user defined type to a pointer to
another user defined type, which is allowed.

--
Erik Wikström
Oct 13 '08 #10
On Oct 13, 5:48 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-10-13 11:27, James Kanze wrote:
On Oct 12, 4:08 pm, peter koch <peter.koch.lar...@gmail.comwrote:
On 12 Okt., 13:16, Alex Vinokur <ale...@users.sourceforge.netwrote:
Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
Only a reinterpret_cast works: the two types are unrelated.
Why do you want to do this? It would be better to fix your
design instead.
Reinterpret_cast doesn't work. You can't do a reinterpret_cast
to or from a user defined type, and instantiations of std::pair
are considered user defined types.
He wanted to cast from a pointer to a user defined type to a
pointer to another user defined type, which is allowed.
Yep. I missed the trailing * in the original posting. In that
case, you can use reinterpret_cast to replace a compile time
error with runtime undefined behavior. Otherwise, you do need
to create a new object, using the technique I described.

(Also, it seems sort of strange to have pointers to an
std::pair. The object definitely has value semantics.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 13 '08 #11

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

Similar topics

5
4446
by: Marc Schellens | last post by:
I have: char* byteN = "BYTE"; char* byte_fun() { return type_fun< byteN>(); } char* fix_fun() {
6
13234
by: bonham | last post by:
i m a beginner in C++. I have some questions: what is static_cast<unsigned>? i saw someone use static_cast<unsigned> and static_cast<unsigned char> together. why? e.g....
8
3717
by: Earl Purple | last post by:
On VC++.NET it is implemented like this static int __cdecl compare ( const _Elem *_First1, const _Elem *_First2, size_t _Count ) { // compare [_First1, _First1 + _Count) with [_First2, ...)...
13
4619
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar,...
8
4312
by: Marco Costa | last post by:
Hello all, I wrote a simple ODBC wrapper class that used code like this ( not real code, added types for clarification ): char** type bufs = new char* for( int i = 0 ; i < numberOfColumns ;...
0
1452
by: pillbug | last post by:
I am trying to convince the Greta regular expression library to use the following char_traits: struct ignore_case_traits : std::char_traits<char> { static bool eq (const char& x, const char& y)...
5
9177
by: Tom Smith | last post by:
I hardly dare ask this given the furore in another thread over strings and const... My problem is this. I am assured that casting away the constness of the return value of std::string::c_str() is...
4
2367
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual...
0
7205
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,...
0
7093
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...
1
7008
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...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5594
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,...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1521
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 ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.