out/ref params

  • Thread starter Thread starter Simon Cheng
  • Start date Start date
S

Simon Cheng

Hi,

Does MC++ support out/ref param semantics (as C# does)? The MC++ spec
doesn't mention about this, so supposedly they are not supported?

Thanks,
Simon
 
Simon said:
Does MC++ support out/ref param semantics (as C# does)? The MC++ spec
doesn't mention about this, so supposedly they are not supported?

Yes.

For ref:
MC++: void DoSth(
Int32* pn,
System::String** ps);

C#: void DoSth(ref Int32 pn, ref System.String ps);


For out:
MC++: void DoSth(
[System::Runtime::InteropServices::OutAttribute] Int32* pn,
[System::Runtime::InteropServices::OutAttribute] System::String** ps);

C#: void DoSth(out Int32 pn, out System.String ps);


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
They don't enforce C#'s out/ref semantics. For example, the following MC++
code compiles fine:

void F1([System::Runtime::InteropServices::OutAttribute] Int32* pi) {}
void F2(Int32* pi) {}

int main()
{
Int32 i, j;
F1(&i);
F2(&j);
}

But the equivalent C# code would fail compilation because:

- F1() doesn't write to *pi before returning.
- j is not written before passed to F2().

Simon

Jochen Kalmbach said:
Simon said:
Does MC++ support out/ref param semantics (as C# does)? The MC++ spec
doesn't mention about this, so supposedly they are not supported?

Yes.

For ref:
MC++: void DoSth(
Int32* pn,
System::String** ps);

C#: void DoSth(ref Int32 pn, ref System.String ps);


For out:
MC++: void DoSth(
[System::Runtime::InteropServices::OutAttribute] Int32* pn,
[System::Runtime::InteropServices::OutAttribute] System::String** ps);

C#: void DoSth(out Int32 pn, out System.String ps);


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
Simon said:
They don't enforce C#'s out/ref semantics. For example, the following
MC++ code compiles fine:

void F1([System::Runtime::InteropServices::OutAttribute] Int32*
pi) {}

Hä !?
This is wrong... it will compile but it is wrong..
you have to return an value, for example "*pi = 12"...
void F2(Int32* pi) {}

This is ok.
But the equivalent C# code would fail compilation because:

- F1() doesn't write to *pi before returning.
- j is not written before passed to F2().

Hä !?
Please provide a full example...

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
My original question was if MC++ supports C#'s out/ref semantics, which I
also meant if MC++ compiler _enforces_ the restrictions to out/ref as C#
compiler does -- that part I think I was not clear enough up-front. For
example, the following C# code gives compilation errors, which is as
expected:

class MyApp
{
static void F1(out int i) {} // Error CS0177
static void F2(ref int i) {}

public static void Main()
{
int i, j;
F1(out i);
F2(ref j); // Error CS0165
}
}

Error CS0177: The out parameter 'i' must be assigned to before control
leaves the current method
Error CS0165: Use of unassigned local variable 'j'

But the following MC++ code compiles without any error:

#using <mscorlib.dll>

void F1([System::Runtime::InteropServices::OutAttribute] int* pi) {}
void F2(int* pi) {}

int main()
{
int i, j;
F1(&i);
F2(&j);
}

So, supposedly MC++ has no facility that is semantically equivelant to C#'s
out/ref?

Simon

Jochen Kalmbach said:
Simon said:
They don't enforce C#'s out/ref semantics. For example, the following
MC++ code compiles fine:

void F1([System::Runtime::InteropServices::OutAttribute] Int32*
pi) {}

Hä !?
This is wrong... it will compile but it is wrong..
you have to return an value, for example "*pi = 12"...
void F2(Int32* pi) {}

This is ok.
But the equivalent C# code would fail compilation because:

- F1() doesn't write to *pi before returning.
- j is not written before passed to F2().

Hä !?
Please provide a full example...

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
Simon said:
But the following MC++ code compiles without any error:

#using <mscorlib.dll>

void F1([System::Runtime::InteropServices::OutAttribute] int* pi)
{} void F2(int* pi) {}

int main()
{
int i, j;
F1(&i);
F2(&j);
}

So, supposedly MC++ has no facility that is semantically equivelant to
C#'s out/ref?

Only the compiler does not ENFORCE this. But this is common to all C++
compilers that you can do more or less if you want...

C++ also does not restrict casting... or use of void-pointers...

If you want a type-safe and "language-safe" language you must use C# (or
any other langugate that supports what you want...)

Recapitulating we can say: C++ needs a higher learning curve that C#, and
you can do musch more "bad things" than with C#.

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
That's the reason I am wondering if MC++ (as opposed to C++) can enforce
C#'s out/ref semantic, as MC++ has already provided some other C# facilities
via additional keywords. I am working on some code that needs to be done in
MC++, and equivalent facility to enforce out/ref would be nice.

Simon

Jochen Kalmbach said:
Simon said:
But the following MC++ code compiles without any error:

#using <mscorlib.dll>

void F1([System::Runtime::InteropServices::OutAttribute] int* pi)
{} void F2(int* pi) {}

int main()
{
int i, j;
F1(&i);
F2(&j);
}

So, supposedly MC++ has no facility that is semantically equivelant to
C#'s out/ref?

Only the compiler does not ENFORCE this. But this is common to all C++
compilers that you can do more or less if you want...

C++ also does not restrict casting... or use of void-pointers...

If you want a type-safe and "language-safe" language you must use C# (or
any other langugate that supports what you want...)

Recapitulating we can say: C++ needs a higher learning curve that C#, and
you can do musch more "bad things" than with C#.

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
Simon said:
They don't enforce C#'s out/ref semantics. For example, the following MC++
code compiles fine:

void F1([System::Runtime::InteropServices::OutAttribute] Int32* pi) {}
void F2(Int32* pi) {}

int main()
{
Int32 i, j;
F1(&i);
F2(&j);
}

But the equivalent C# code would fail compilation because:

- F1() doesn't write to *pi before returning.
- j is not written before passed to F2().

Compilers are free to ignore the OutAttribute. The C# language respects the
attribute, and C# disallows the F2 call because of its "definite assignment"
requirement. C++ supports neither feature.
 
Back
Top