What is the difference between int and System::Int32

Some may say identical, at least that’s what the Visual C++ compiler tells you at the first glance  when you turn on /oldsyntax

public __gc class Class1
{
public:
void F1(int a){}
void F1(System::Int32 a){} //Error    2    error  2535:  void Class1::F1(int)’ : member function already defined or declared
}

Okay, so if I add & to the parameter types I should get the same error right?void F1(int&a){}
void F1(System::Int32& a){}

Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped

Wait, didn’t I get an error just now? You are right, somehow the compiler treat the two types differently when passing by reference. look at the function signatures in MSIL. Int32 is passed by reference as expected

.method public instance void F1(int32& modopt([mscorlib]System.Runtime.CompilerServices.IsImplicitlyDereferenced) a) cil managed
{
    .maxstack 0
    L_0000: ret
}

and int is passed by address (the behavior is different if you switch to new syntax)

.method public instance void F1(int32* modopt([mscorlib]System.Runtime.CompilerServices.IsImplicitlyDereferenced) a) cil managed
{
    .maxstack 0
    L_0000: ret
}

I guess it does not make a big deal right?

Until you have an interface assembly that is compiled in anower compiler and want to implement the interface. Now you have a hard time to figure out how to declare the signature in the new syntax.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.