*long vs Int32 tracking reference

R

rob

Hi,

I'm a bit confused by the mapping of the native types int and long to
UInt32. I'm trying to bind a tracking reference to a native value type
and it works for Int32 % -> int but not for Int32 % -> long.

This page http://msdn2.microsoft.com/en-gb/library/0wf2yk2k(VS.80).aspx
states that both int & long map to Int32.

The following code prints "Hello World 1" rather than the expected
"Hello World 32". If I change the 2 long types to ints (as commented)
the code works as expected, printing "Hello World 32".

using namespace System;

void fUniversalArg(Int32 %var)
{
var = 32;
}

void fNativeArg(long *pVar /* int *pVar */)
{
fUniversalArg((Int32)*pVar);
}

int main(array<System::String ^> ^args)
{
// int var = 1;
long var = 1;
fNativeArg(&var);

Console::WriteLine(L"Hello World " + var);
return 0;
}

Any thoughts - I guess I am mising something...

Cheers
Rob
 
T

Tamas Demjen

This page http://msdn2.microsoft.com/en-gb/library/0wf2yk2k(VS.80).aspx
states that both int & long map to Int32.

That's correct, but it just means that long and int are both 32-bit
signed integers under Win32 and Win64, and they're both compatible with
System::Int32. But that doesn't mean logn and int are the same exact
type. long is not simply a typedef for int. Even in native code, they're
not interchangeable types:

void f(int& value);
void test()
{
long value;
f(value); // error or warning message
}

My C++ reports an error "cannot convert parameter 1 from 'long' to
'int&'. My Borland compiler remports a warning and creates a temporary
object, whose value gets discarded after the function call.
void fNativeArg(long *pVar /* int *pVar */)
{
fUniversalArg((Int32)*pVar);

Here the compiler creates a silent temporary object. fNativeArg modifies
your temporary, which gets discarded right away. Your code is
essentially equivalent with this:
void fNativeArg(long *pVar /* int *pVar */)
{
Int32 value = *pVar;
fUniversalArg(value);
}

Note how pVar will never get updated.

What you want is this:

void fNativeArg(long *pVar /* int *pVar */)
{
fUniversalArg((Int32%)*pVar);
}

After adding the % symbol, your program will print 32.

Tom
 
R

Rob

But that doesn't mean logn and int are the same exact
type. long is not simply a typedef for int. Even in native code, they're
not interchangeable types:

Yes, I understood that...
Here the compiler creates a silent temporary object. fNativeArg modifies
your temporary, which gets discarded right away. Your code is
essentially equivalent with this:

.... but I hadn't thought through to what the compiler was generating.
What you want is this:

void fNativeArg(long *pVar /* int *pVar */)
{
fUniversalArg((Int32%)*pVar);

}After adding the % symbol, your program will print 32.

Thanks! I knew I was probably doing something daft.

Cheers
Rob
 

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

Top