Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>*

Reply
Thread Tools

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

 
 
Alex Vinokur
Guest
Posts: n/a
 
      10-12-2008
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
 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      10-12-2008
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
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      10-12-2008
On Oct 12, 7:16*pm, Alex Vinokur <(E-Mail Removed)> 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 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
 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      10-12-2008
On Oct 12, 2:22*pm, Barry <(E-Mail Removed)> wrote:
> On Oct 12, 7:16*pm, Alex Vinokur <(E-Mail Removed)> 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 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
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      10-12-2008
On Oct 12, 9:05*pm, Alex Vinokur <(E-Mail Removed)> wrote:
> On Oct 12, 2:22*pm, Barry <(E-Mail Removed)> wrote:
>
>
>
> > On Oct 12, 7:16*pm, Alex Vinokur <(E-Mail Removed)> 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 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

 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      10-12-2008
On 12 Okt., 13:16, Alex Vinokur <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-13-2008
On Oct 12, 4:08 pm, peter koch <(E-Mail Removed)> wrote:
> On 12 Okt., 13:16, Alex Vinokur <(E-Mail Removed)> wrote:
> > 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:air
are considered user defined types.

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

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

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Vidar Hasfjord
Guest
Posts: n/a
 
      10-13-2008
On Oct 12, 2:05*pm, Alex Vinokur <(E-Mail Removed)> wrote:
> 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_t> pair1;
typedef pair <unsigned char*, size_t> pair2;

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

Regards,
Vidar Hasfjord
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      10-13-2008
On 2008-10-13 11:27, James Kanze wrote:
> On Oct 12, 4:08 pm, peter koch <(E-Mail Removed)> wrote:
>> On 12 Okt., 13:16, Alex Vinokur <(E-Mail Removed)> wrote:
>> > 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:air
> 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-13-2008
On Oct 13, 5:48 pm, Erik Wikstrm <(E-Mail Removed)> wrote:
> On 2008-10-13 11:27, James Kanze wrote:
>
> > On Oct 12, 4:08 pm, peter koch <(E-Mail Removed)> wrote:
> >> On 12 Okt., 13:16, Alex Vinokur <(E-Mail Removed)> wrote:
> >> > 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:air
> > 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:air. The object definitely has value semantics.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
unsigned int const does not match const unsigned int Timo Freiberger C++ 3 10-30-2004 07:02 PM



Advertisments