473,473 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++/CLI quirk passing properties as reference parameters

The following example illustrates something that's taken me a while to
suss out (debugging someone else's code :( )...

The crux is that the code compiles cleanly at W4, and the issue is
that I'm passing a property by reference to a function that's trying
to modify it.

Needless to say the result isn't what was intended as the property
doesn't get modified.

So... ought the compiler to allow this?
At the very least I'd hope to have a warning message.

#include "stdafx.h"
using namespace System;

ref struct MyStruct
{
int m_i1;
property int m_i2;
};

void f1( int % i )
{
i++;
}

int main(array<System::String ^^/*args*/)
{
MyStruct s;

s.m_i1 = 1;
s.m_i2 = 2;

Console::WriteLine(L"m_i1 is:" + s.m_i1.ToString() + " m_i2
is: " + s.m_i2.ToString() );

f1( s.m_i1 );
f1( s.m_i2 );

Console::WriteLine(L"m_i1 is:" + s.m_i1.ToString() + " m_i2
is: " + s.m_i2.ToString() );
return 0;
}

Dave Lowndes
Feb 15 '07 #1
7 5374

"David Lowndes" <Da****@example.invalidwrote in message
news:44********************************@4ax.com...
The following example illustrates something that's taken me a while to
suss out (debugging someone else's code :( )...

The crux is that the code compiles cleanly at W4, and the issue is
that I'm passing a property by reference to a function that's trying
to modify it.

Needless to say the result isn't what was intended as the property
doesn't get modified.

So... ought the compiler to allow this?
At the very least I'd hope to have a warning message.
As you say, it ought to be generating a warning such as "passing temporary
by non-const reference" or "taking address of temporary", which I've indeed
seen on some of my own code before.

Does the code use pragma warning anywhere?
>
#include "stdafx.h"
using namespace System;

ref struct MyStruct
{
int m_i1;
property int m_i2;
};

void f1( int % i )
{
i++;
}

int main(array<System::String ^^/*args*/)
{
MyStruct s;

s.m_i1 = 1;
s.m_i2 = 2;

Console::WriteLine(L"m_i1 is:" + s.m_i1.ToString() + " m_i2
is: " + s.m_i2.ToString() );

f1( s.m_i1 );
f1( s.m_i2 );

Console::WriteLine(L"m_i1 is:" + s.m_i1.ToString() + " m_i2
is: " + s.m_i2.ToString() );
return 0;
}

Dave Lowndes

Feb 15 '07 #2
>Does the code use pragma warning anywhere?

That particular code was a VS2005 generated C++/CLI console project, I
set W4 in the project settings to see if it would identify an issue -
it didn't :(

I can't see anything in the generated sources that would drop the
warning level.

Dave
Feb 15 '07 #3

"David Lowndes" <Da****@example.invalidwrote in message
news:6r********************************@4ax.com...
Does the code use pragma warning anywhere?

That particular code was a VS2005 generated C++/CLI console project, I
set W4 in the project settings to see if it would identify an issue -
it didn't :(

I can't see anything in the generated sources that would drop the
warning level.

Dave
Looks like an issue because .NET has no concept of a constant reference.

Part of your problem is that you're accepting a tracking reference. Since
int is a value type, you can use & instead, which works as expected and
generates an error.

void f1(int% ri)
{
ri = 0;
}

void f2(int& ri)
{
ri = 0;
}

int get(void)
{
return 10;
}

int main(void)
{
f1(get()); // no error
f2(get()); // error
}

However, the documentation says that % should work as well (See article: How
to: Write Template Functions that Take Native, Value, or Reference
Parameters), so this looks like a genuine bug. I think we should look at
the generated MSIL, I suspect that implicit boxing is taking place.
..method assembly static int32 main() cil managed
{
.maxstack 1
.locals (
[0] int32 num1)
L_0000: call int32 <Module>::get()
L_0005: stloc.0
L_0006: ldloca.s num1
L_0008: call void <Module>::f1(int32&)
L_000d: ldc.i4.0
L_000e: ret
}

Nope, no boxing. Just a temporary passed by non-const reference. Seems to
be a result of Microsoft's choice not to implement const safety in managed
code. There's no way the compiler can know from the function prototype
whether the argument will be changed. Seems like it should then be a
warning instead of an error.

However, even adding the OutAttribute on the parameter, the code doesn't
generate even a warning although it clearly should be an error to use a
temporary as an out-only parameter.

File a bug report and I'll vote for and validate it, here's your minimal
repro code (compile with /clr:safe):

void f1([System::Runtime::InteropServices::Out] int% ri)
{
ri = 0;
}

int get( void )
{
return 10;
}

int main(void)
{
f1(get());
}
Feb 15 '07 #4
>Looks like an issue because .NET has no concept of a constant reference.
>
Part of your problem is that you're accepting a tracking reference. Since
int is a value type, you can use & instead, which works as expected and
generates an error.
The real code issue was with a DateTime variable/property. Bugged it
anyhow:

https://connect.microsoft.com/Visual...dbackID=258100

Dave
Feb 15 '07 #5
>https://connect.microsoft.com/Visual...dbackID=258100

Resolved (Won't Fix) with no comments to explain why or if they ever
will consider it :(

Dave
Feb 17 '07 #6

"David Lowndes" <Da****@example.invalidwrote in message
news:v1********************************@4ax.com...
https://connect.microsoft.com/Visual...dbackID=258100

Resolved (Won't Fix) with no comments to explain why or if they ever
will consider it :(
Some of the folks on there seem genuinely interested in making the products
better, while there are at least a few only interested in improving their
statistics (bugs closed/week)
Dave

Feb 19 '07 #7
>https://connect.microsoft.com/Visual...dbackID=258100
>>
Resolved (Won't Fix) with no comments to explain why or if they ever
will consider it :(

Some of the folks on there seem genuinely interested in making the products
better, while there are at least a few only interested in improving their
statistics (bugs closed/week)
Yeah, the inconsistency in responses to reports is tremendous.

I've re-opened it, let's see if that generates some reasonable
feedback.

Dave
Feb 20 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.