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

A

Alex Vinokur

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
 
E

Erik Wikström

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.
 
B

Barry

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?
 
A

Alex Vinokur

  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
 
B

Barry

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.
 
P

peter koch

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
 
J

James Kanze

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 ) ;
 
V

Vidar Hasfjord

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
 
E

Erik Wikström

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.
 
J

James Kanze

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.)
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top