473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Expected? Parameters of type Int32 (and thus __value enum) implicitly converted to DateTime

We had a function with the signature:

Function(Object *, DateTime, DateTime, EnumType)

and then changed the class some and there was a 4 parameter function with
the signature:

Function(Object *, DateTime, DateTime, DateTime)

The previous code had not been changed and to our surprise the function
compiled without error. I did a little testing and found any Int32 value
would be accepted as a DateTime parameter. Why is this not a compiler
error?

This definitely would have led to bugs would we have not known about the
functions that needed to be changed. There isn't even a constructor that
takes an Int32 specifically (though there is one for Int64), so the result
of this conversion is less than obvious. Is this documented somewhere or a
bug?

Christopher Crooker
Acquist Incorporated
Nov 16 '05 #1
3 3199
"Christopher Crooker" <ch***@AcquistIncorporated.com> wrote:
We had a function with the signature:

Function(Object *, DateTime, DateTime, EnumType)

and then changed the class some and there was a 4 parameter function with
the signature:

Function(Object *, DateTime, DateTime, DateTime)

The previous code had not been changed and to our surprise the function
compiled without error. I did a little testing and found any Int32 value
would be accepted as a DateTime parameter. Why is this not a compiler
error?
I don't know the definition of 'DateTime', but I
suspect it has a non-explicit constructor taking
and 'Int32'?
If so, the bug is in this class' design.
Christopher Crooker
Acquist Incorporated


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers org

"My hair style calls into immediate question all my judgements."
Scott Meyers
(http://www.google.de/groups?selm=MPG...ws.hevanet.com)
Nov 16 '05 #2
Okay, that makes sense. I was overlooking the obvious here. However, does
anyone know if this type of automatic conversion between an Enum type and
something expecting an Int32 will always work? I read somewhere, a
knowledge base article maybe, that eventually there would be an
implemntation of Enums in Managed C++ that can have a different base type
(Short, Int64, etc.) but will this implemntation also make them seperate
types as far as the compiler is concerned or will Enum values always be
treated as equivalent to their base types native counterpart?

Thanks,
Chris

"Tomas Restrepo (MVP)" <to****@mvps.org> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
Hi Christopher,
We had a function with the signature:

Function(Object *, DateTime, DateTime, EnumType)

and then changed the class some and there was a 4 parameter function with the signature:

Function(Object *, DateTime, DateTime, DateTime)

The previous code had not been changed and to our surprise the function
compiled without error. I did a little testing and found any Int32 value would be accepted as a DateTime parameter. Why is this not a compiler
error?

This definitely would have led to bugs would we have not known about the
functions that needed to be changed. There isn't even a constructor that takes an Int32 specifically (though there is one for Int64), so the result of this conversion is less than obvious. Is this documented somewhere
or a
bug?

Looking at the generated code is easy to see what is happening: The Int32
value is getting extended to an Int64 value, then the DateTime constructor
you're mentioning is getting invoked. Unintuive, for sure, but perfectly
valid according to C++ rules.

--
Tomas Restrepo
to****@mvps.org

Nov 16 '05 #3
Hi Christopher,
Okay, that makes sense. I was overlooking the obvious here. However, does anyone know if this type of automatic conversion between an Enum type and
something expecting an Int32 will always work?
Compile, yes. Work? depends.
I read somewhere, a
knowledge base article maybe, that eventually there would be an
implemntation of Enums in Managed C++ that can have a different base type
(Short, Int64, etc.)
That's already implemented. For example:
public __value enum E : __int64
{
E1 = 1,
E2 = 2
};

but will this implemntation also make them seperate
types as far as the compiler is concerned or will Enum values always be
treated as equivalent to their base types native counterpart?


AFAIK, yes. Notice however that C++ usually allows silent narrowing
conversions on integral types, with possibly undefined results (well, not
undefined, but implementation defined anyway).

--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #4

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

Similar topics

3
by: Edward Diener | last post by:
In an assembly I have a __value enum with some enumerated constants, ie. namespace X { public __value enum MyEnum { ValueA, ValueB }; }
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
2
by: Mark | last post by:
I created a test to check the execution time difference between executing a SQL Server stored procedured using explicit parameters versus not. In one case I created new SqlParameters in the code,...
14
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 =...
0
by: Edward Diener | last post by:
Why is there a prohibition of instantiating __value type enums and classes within __nogc classes ? After all __value types are not managed by the GC and built-in __value types, such as 'int', are...
2
by: Patrick Olurotimi Ige | last post by:
I converted the code below from VB.NET to C# cos i have to add it to a C# application!! But i'm getting the error:- System.Data.SqlClient.SqlCommand.Parameters' denotes a 'property' where a...
4
by: mark.olszowka | last post by:
I am writing a generic property table as part of my application property_name property_type property_value All information is stored in the database as strings. In my application that...
59
by: peter.tornqvist | last post by:
Maybe I'm stupid or maybe I am missing the obvious, but I can't find a way to define a type alias in C# for primitive types (class and interfaces I can inherit from, no problem). I.e I want to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
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...

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.