473,666 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compiler resolves to wrong overloaded constructor or method

Is this a bug in the C# compiler or CLR runtime?

enum MyEnum { ZERO = 0, ONE = 1, TWO = 2 }
class Foo {
public Foo(string,obje ct) { ... }
public Foo(string,MyEn um) { ... }
}

Foo f = new Foo("", 0); // Uses Foo(string,MyEn um) constructor instead of
Foo(string,obje ct)
Foo f = new Foo("", 0.0); // Uses Foo(string,MyEn um) constructor instead
of Foo(string,obje ct)
Foo f = new Foo("", (int)0); // Uses Foo(string,MyEn um) constructor
instead of Foo(string,obje ct)
BUT
Foo f = new Foo("", 1 ); // Uses Foo(string,obje ct)
Foo f = new Foo("", (object)(int)0 ); // Uses Foo(string,obje ct) with
object type Int32
Foo f = new Foo("", (double)0.0 ); // Uses Foo(string,obje ct)

Actual case was from a ADO.NET Data Provider's PrvParameter constructors!
Same issue came up resolving the various overloads of
PrvParameterCol lection.Add!

Anybody else seen this?
Philippe
Nov 16 '05 #1
30 1726
Philippe,

Haven't seen it.

How do you know it's using the Foo(string,MyEn um) constructor when you do a
new Foo("",0)? Are you seeing this when you single-step through the code?
If not, you should single-step with the debugger to make sure what you think
is executing is really executing. If you single-step and *still* see this
behavior, then I would rule out a bug in the debug display (it's happened in
the past, in unpatched CLR 1.0) by putting DebugWriteLine( ) statements into
the constructors and making sure they really DO execute as the debugger
shows.

--Bob

"Philippe Bertrand" <my*****@ianywh ere.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Is this a bug in the C# compiler or CLR runtime?

enum MyEnum { ZERO = 0, ONE = 1, TWO = 2 }
class Foo {
public Foo(string,obje ct) { ... }
public Foo(string,MyEn um) { ... }
}

Foo f = new Foo("", 0); // Uses Foo(string,MyEn um) constructor instead of Foo(string,obje ct)
Foo f = new Foo("", 0.0); // Uses Foo(string,MyEn um) constructor instead of Foo(string,obje ct)
Foo f = new Foo("", (int)0); // Uses Foo(string,MyEn um) constructor
instead of Foo(string,obje ct)
BUT
Foo f = new Foo("", 1 ); // Uses Foo(string,obje ct)
Foo f = new Foo("", (object)(int)0 ); // Uses Foo(string,obje ct) with
object type Int32
Foo f = new Foo("", (double)0.0 ); // Uses Foo(string,obje ct)

Actual case was from a ADO.NET Data Provider's PrvParameter constructors!
Same issue came up resolving the various overloads of
PrvParameterCol lection.Add!

Anybody else seen this?
Philippe

Nov 16 '05 #2

"Philippe Bertrand" <my*****@ianywh ere.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Is this a bug in the C# compiler or CLR runtime?

enum MyEnum { ZERO = 0, ONE = 1, TWO = 2 }
class Foo {
public Foo(string,obje ct) { ... }
public Foo(string,MyEn um) { ... }
}

Foo f = new Foo("", 0); // Uses Foo(string,MyEn um) constructor instead of Foo(string,obje ct)
Foo f = new Foo("", 0.0); // Uses Foo(string,MyEn um) constructor instead of Foo(string,obje ct)
Foo f = new Foo("", (int)0); // Uses Foo(string,MyEn um) constructor
instead of Foo(string,obje ct)
BUT
Foo f = new Foo("", 1 ); // Uses Foo(string,obje ct)
Foo f = new Foo("", (object)(int)0 ); // Uses Foo(string,obje ct) with
object type Int32
Foo f = new Foo("", (double)0.0 ); // Uses Foo(string,obje ct)

I reproduced it. Very odd, especially the difference in behavior between
0.0 and (double)0.0, since 0.0 is already a double.
Nov 16 '05 #3
hi philippe,

see 7.4.2.2 of the C# language specification.
Foo f = new Foo("", 0); // Uses Foo(string,MyEn um) constructor instead of
Foo(string,obje ct)
there exists an implicit convertion between int and MyEnum.
see 6.1.3 of the C# lang specification.
Foo f = new Foo("", 0.0); // Uses Foo(string,MyEn um) constructor instead
of Foo(string,obje ct)
there exists an implicit convertion between double and int
and MyEnum. see 6.1.3.
Foo f = new Foo("", (int)0); // Uses Foo(string,MyEn um) constructor
instead of Foo(string,obje ct)
there exists an implicit convertion between int and MyEnum.
see 6.1.3.
BUT
Foo f = new Foo("", 1 ); // Uses Foo(string,obje ct)
6.1.3 doesn't apply here.
Foo f = new Foo("", (object)(int)0 ); // Uses Foo(string,obje ct) with
object type Int32
the cast forces Foo(string,obje ct).
Foo f = new Foo("", (double)0.0 ); // Uses Foo(string,obje ct)


the cast disables the implicit int promotion.
since no Foo(string,doub le) exists, the next bext
overloaded method is taken.

bye
rob
Nov 16 '05 #4
Robert,

Can you give correct links to C# spec? (I mean ECMA-334, 2nd ed. (december
2002)).

Alex.
see 7.4.2.2 of the C# language specification.

there exists an implicit convertion between int and MyEnum.
see 6.1.3 of the C# lang specification.

Nov 16 '05 #5
But that is ridiculous!

You are saying to users that for all but these two special constants, the
language behaves on way but for these two magic constants you have to
explicitly cast.

Being in the spec doesn't make it a good idea!

"Robert Jordan" <ro*****@gmx.ne t> wrote in message
news:cg******** *****@news.t-online.com...
hi philippe,

see 7.4.2.2 of the C# language specification.
Foo f = new Foo("", 0); // Uses Foo(string,MyEn um) constructor instead of Foo(string,obje ct)
there exists an implicit convertion between int and MyEnum.
see 6.1.3 of the C# lang specification.
Foo f = new Foo("", 0.0); // Uses Foo(string,MyEn um) constructor instead of Foo(string,obje ct)


there exists an implicit convertion between double and int
and MyEnum. see 6.1.3.
Foo f = new Foo("", (int)0); // Uses Foo(string,MyEn um) constructor
instead of Foo(string,obje ct)


there exists an implicit convertion between int and MyEnum.
see 6.1.3.
BUT
Foo f = new Foo("", 1 ); // Uses Foo(string,obje ct)


6.1.3 doesn't apply here.


Why would there be an implicit conversion between int and MyEnum for the
value 0 but not the value 1?

Note:
int i = 0;
Foo f = new Foo("", i ); // Uses Foo(stirng,obje ct)

This section 6.1.3 seems an extremely bizzard exception!
Foo f = new Foo("", (object)(int)0 ); // Uses Foo(string,obje ct) with
object type Int32


the cast forces Foo(string,obje ct).
Foo f = new Foo("", (double)0.0 ); // Uses Foo(string,obje ct)


the cast disables the implicit int promotion.
since no Foo(string,doub le) exists, the next bext
overloaded method is taken.


Why would the implicit conversion be disabled when casting (double)0.0 but
not when casting (int)0 ?

Philippe
Nov 16 '05 #6
Yes, I'm certain (resulting object state different and stepped through
code). See my reply to Robert Jordan for cause of this magic behaviour.

Philippe
Nov 16 '05 #7
Hi Alex,
Robert,

Can you give correct links to C# spec? (I mean ECMA-334, 2nd ed. (december
2002)).


13.1.3 Implicit enumeration conversions
(6.1.3 MSDN spec)

"An implicit enumeration conversion permits the decimal-integer-literal
0 to be converted to any enum-type"
14.4.2.1 Applicable function member
(7.4.2.2 MSDN spec)

Rob
Nov 16 '05 #8
Thank you!

Alex.
Nov 16 '05 #9
Hi Philippe,
You are saying to users that for all but these two special constants, the
language behaves on way but for these two magic constants you have to
explicitly cast.
There is no magic. The language designers wanted to avoid
constructs like "(SomeEnum) 0", because enums may be
[Flags]-able, and thus they may be "0" w/out having
a literal with the value "0".

Consider this:

if ((val & SomeEnum.Foo) == 0) {
}

W/out this rule, the bool expression has to
be written like:

if ((val & SomeEnum.Foo) == (SomeEnum)0) {
}

This is ugly!

Being in the spec doesn't make it a good idea!


Reading the specs is a good idea, too ;-)

Bye
Rob
Nov 16 '05 #10

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

Similar topics

2
2975
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the Intel compiler it has stopped working. I think the singleton pattern static instance thing may be at the root of the problem, but I'm not sure. I had to add calls to instance() in regCreateFn, which gets the behaviour more in line with what I was...
4
1814
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a memory problem that I'm not readily seeing that lies within the overloaded operator - I think pertaining to the temporary class object that is created and used to return a value of the operands and the pointer variable of the class. Could someone...
2
1682
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically as you can see in the constructor. Class Matris should create a matris by using class Intvektor. So what I want to have is a pointer to an array of Intvektor and in each positionindex in this array will I have a pointer to an array of Intvektor
10
2442
by: john bailo | last post by:
Can a web method be overloaded? To support varying numbers of input parameters?
19
3564
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and one web presentation tier (Foo.WebFiles). The data tier shall only be accessible thru the business tier so I do NOT want a reference to the data tier in the presentation tier. In the business tier I have a class with the name CategoryItem that...
3
1589
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client app with; D m_D = new D(tkn); public class A : MarshalByRefObject public A () <--------------------- public class B : A public B () <----------------------
18
1377
by: John Rusk | last post by:
Hi, I have some weird compiler behaviour, so I'm hoping that one of the gurus here will be able to tell me whether or not it really is a compiler bug. In short, the compiler is saying that it cannot choose between two overloaded versions of a method when, IMHO, one overload is clearly a "better" choice than the other. I've looked through all the rules for overload resolution, and I can't see any justification for the compiler's error...
0
2051
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the increment operator in a class and then defining a derived class and using the derived class. When I try to use the increment operator on an instance of the derived class (as an instance of the derived class, not when used as an instance of the base...
1
2265
by: dileepd | last post by:
Hi Every one, Could you please let me know if you have any clue about following things. 1. Whether type bool acts like a kind of signed int type in g++ on solaris 9(a/c to my invetsigation it is "Yes"). 2. If above question is yes,What needs to be done in order for compiler to overload constructor once with bool and one with integer. Iam asking these question as iam encountering following errors.
0
8448
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
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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
8783
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
8552
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,...
1
6198
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
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1776
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.