473,785 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I think this is a bug...

Here is a case where something that shouldn't compile does. Note below that
Test_Method() only accepts a pointer to Test_Struct as an parameter, but it
will compile with a value instance as a parameter.

For context, I'm using VS C++.NEW 2005 Express with clr:/pure syntax. I
submitted this as a bug report, link below:

http://lab.msdn.microsoft.com/produc...e-640a4de75f39

Source below:

// Test_Compiler.c pp : main project file.

#include "stdafx.h"

using namespace System;

value struct Test_Struct
{
long x ;
} ;

ref class My_Class
{
public:

My_Class() {}
~My_Class() {}

private:

void Test_Compiler()
{
Test_Struct info ;

Test_Method( info ) ; // shouldn't compile, but does!!!
Test_Method( %info ) ; // ok
}

void Test_Method( Test_Struct^ info ) {}
} ;

int main(array<Syst em::String ^> ^args)
{
return 0 ;
}

[==P==]

Dec 6 '05 #1
3 939
"Peter Oliphant" <po*******@Roun dTripInc.com> wrote

value struct Test_Struct
{
long x ;
} ;
[..] Test_Struct info ;

Test_Method( info ) ; // shouldn't compile, but does!!!
Test_Method( %info ) ; // ok
}

void Test_Method( Test_Struct^ info ) {}
} ;

I think it's feature. That's "implicit boxing". There's
a boxing conversion from every V to V^ to support
certain types of polymorphism.

For instance, you couldn't write
Console::WriteL ine( info );
without it.

You can think of every Value type having a magic
member
value struct V { V^ operator() const; };

Now there's a conversion sequence from gclvalue V
to Object^ via the boxing conversion followed by
the base class handle conversion.

-hg
Dec 6 '05 #2
Ok, I'll buy that. Sort of. BUT --- doesn't this feature reverse the concept
of type-checking? One of the reasons for passing something by value is so
the original can't be changed. Doesn't this feature violate that concept?

In my example code, the call Test_Method(inf o) looks like it will pass a
value. But Test_Method is passed a pointer, so it can validly be written
with changing what it is passed as a legal method. For example:

void Test_Method( Test_Struct^ info )
{
info->x = 1 ;
}

I just tried this, and if passed the value the code doesn't change the value
of 'x', but if passed a pointer it does. This caused me a half-day of
looking at the code, since I saw the value change in the method and assumed
it remained changed outside the method. But when I finally looked at 'info'
outside the call it had changed, at which point I realized I had forgotten
the '%' and was passing the value instead of the pointer.

AT THE VERY LEAST THIS SHOULD GENERATE A LOW-LEVEL WARNING.

This also brings up the concept that If I HAD overloaded Test_Method() with
a version that DID take a value instead of a pointer, note that my code
would start executing in this new version all of a sudden. So code that
'worked' because it was incorrectly going to a method assuming a pointer now
goes to the new one made for a value type, and the code stops working for
reasons that seem bizarre. And now one must look at code that has not been
changed but is reacting differently since some of the calls it it have been
're-routed' without any warning.

These scenario's couldn't happen if this flagged an error (or a warning)
instead of being a feature...

[==P==]

"Holger Grund" <ho**********@r emove.ix-n.net> wrote in message
news:%2******** *********@TK2MS FTNGP12.phx.gbl ...
"Peter Oliphant" <po*******@Roun dTripInc.com> wrote

value struct Test_Struct
{
long x ;
} ;

[..]
Test_Struct info ;

Test_Method( info ) ; // shouldn't compile, but does!!!
Test_Method( %info ) ; // ok
}

void Test_Method( Test_Struct^ info ) {}
} ;

I think it's feature. That's "implicit boxing". There's
a boxing conversion from every V to V^ to support
certain types of polymorphism.

For instance, you couldn't write
Console::WriteL ine( info );
without it.

You can think of every Value type having a magic
member
value struct V { V^ operator() const; };

Now there's a conversion sequence from gclvalue V
to Object^ via the boxing conversion followed by
the base class handle conversion.

-hg

Dec 6 '05 #3
"Peter Oliphant" <po*******@Roun dTripInc.com> wrote
Ok, I'll buy that. Sort of. BUT --- doesn't this feature reverse the
concept of type-checking? One of the reasons for passing something by
value is so the original can't be changed. Doesn't this feature violate
that concept?

Not necessarily. You really can't tell whether the value is copied
only from the expression at the call site.

For instance, if the declaration was
void Test_Method( Test_Struct% info);
there wouldn't be a copy involved.

Nonetheless, I agree implicit boxing is dangerous and it
shouldn't be in the language IMHO. But I'm afraid you need
to convince someone else.

FWIW, I use a helper function box or box_cast to do an
explicit cast.

-hg
Dec 6 '05 #4

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

Similar topics

23
1906
by: JDeats | last post by:
Just spent some time browsing around here: http://msdn.microsoft.com/Longhorn/ I can see the benefits from WinFS (as long as we tag all in-coming data this should be nice, tagging everything could be a tedious process though) but I haven't been able to pick up the practical advantages of XAML. From my assessment, XAML will provide separation of UI logic from business rules/procedures for applications. It seems like an HTML of sorts for...
3
2448
by: Angel | last post by:
Hello again (and again, and again...) I think I'm getting closer to solving my initial problem of calling unmanaged code. I managed to call the functions with user-defined structs w/o getting any run-time exceptions. The problem is that I'm receiving a Failure return (it returns 1 = "Failure"). I have a feeling it may be that the function's not reading the struct correctly. This time I decided to display all the code (it's very short)....
29
2590
by: GhostInAK | last post by:
I'm seeing a terribly distubing number of questions that have no purpose in existing. As an example: How do I change the position of a stream? Hmm.. Could it be some method on the stream itsself? Probably. My advice, pleading advice, is that you USE YOUR DAMN BRAINS before spewing your crap on here. Think about what you are doing. If your question is "How do I do X with class/object Y?" Then why not open up the Obejct Browser and...
1
1381
by: Google Questions | last post by:
After NorthKorean problem, do you find any similarities between your country and North Korea ? What did you teach your kid generation about how they should think about the world ? If you are a researcher, after some of your papers got accepted in some journals, HAVE YOU EVER THOUGHT THAT YOUR PAPERS COULD HELP DEVELOP YOUR WHOLE COUNTRY SO THAT YOU THEN IMMIDIATELY DON'T EVER PAY A SHIT TO ENGLISH LANGUAGE ? THINKING THAT YOUR PAPERS...
0
2195
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10346
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8982
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2887
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.