Casting between const type** and type**

K

Kevin L

I have a function declared as such:

void func(const int** p)
{
}

If I have calling body
void callfunc()
{
int** p;
func(p);
}

VS.NET (2002) fails to compile:
error C2664: 'const_test' : cannot convert parameter 1 from 'int ** '
to 'const int ** '

Is this expected behavior? I can resolve this error by using
const_cast to force the type change. So I'm asking out of curiosity
since I see nothing wrong with the code as is intuitively.
 
C

cody

when you have to use a cast between const and non-const variables you always
have a design flaw.
but in your example you only have to change one line of code to correct
that:

void callfunc()
{
const int** p; // make it const
func(p);
}

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu
[noncommercial and no fucking ads]
 
R

Ron Natalie

Kevin L said:
VS.NET (2002) fails to compile:
error C2664: 'const_test' : cannot convert parameter 1 from 'int ** '
to 'const int ** '

Is this expected behavior? I can resolve this error by using
const_cast to force the type change. So I'm asking out of curiosity
since I see nothing wrong with the code as is intuitively.

You can't do that. The code is not type safe.
Imagine if the following were legal:

const int i = 5;

void func(const int** p) {
*p = &i;
}

void callfunc() {
int* p;
func(&p): // not legal, but pretend it was.
*p = 5; // legal. BOOM!
}

Note that the line marked BOOM would allow the const int i to be changed.
ALlowing int** to be passed ton const int** allows the const to be removed.
 
A

Andrey Tarasevich

Kevin said:
I have a function declared as such:

void func(const int** p)
{
}

If I have calling body
void callfunc()
{
int** p;
func(p);
}

VS.NET (2002) fails to compile:
error C2664: 'const_test' : cannot convert parameter 1 from 'int ** '
to 'const int ** '

Is this expected behavior? I can resolve this error by using
const_cast to force the type change. So I'm asking out of curiosity
since I see nothing wrong with the code as is intuitively.

It is in the FAQ.

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.15
 
K

Kevin L

Ron Natalie said:
The other way to fix it would be to define func as:
void func(const int* const* p) { ...

This will prevent the possible "loss of const" that prevents the implicit conversion
from happening.


Just when I thought I had const figured out....

I simplified the offending code in question for posting, the actual
code comes from, for those interested, ConvertUTF functions from
www.unicode.org. The prototype is:

ConversionResult ConvertUTF16toUTF8 (
const UTF16** sourceStart, const UTF16* sourceEnd,
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);

Now I have a buffer I want to convert, let's call it UTF16* buf, which
would not be const since I've been using the pointer to modify the
buffer. If I understood the explanations correctly, the two solutions
provided above won't do the trick (*sourceStart will be modified
within the function). What's the most graceful way to do the pass, if
there is one?
 
R

Ron Natalie

Kevin L said:
I simplified the offending code in question for posting, the actual
code comes from, for those interested, ConvertUTF functions from
www.unicode.org. The prototype is:

ConversionResult ConvertUTF16toUTF8 (
const UTF16** sourceStart, const UTF16* sourceEnd,
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);

You need to call it like this:

UTF16* my_buf[100];

const UTF16* = my_buf;
ConvertUTF16toUTF8(&my_buf, ...
 
R

Ron Natalie

Ron Natalie said:
Kevin L said:
I simplified the offending code in question for posting, the actual
code comes from, for those interested, ConvertUTF functions from
www.unicode.org. The prototype is:

ConversionResult ConvertUTF16toUTF8 (
const UTF16** sourceStart, const UTF16* sourceEnd,
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);

You need to call it like this:

UTF16* my_buf[100];

const UTF16* = my_buf;
ConvertUTF16toUTF8(&my_buf, ...
My brain spazzed. That should read
UTF16* my_buf[100];
const UTF16* my_cptr = my_buf;
ConvertUTF16toUTF8(&my_cptr, ...
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top