472,959 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Explicit conversion from SomeType* to IntPtr

Why explicit conversion from SomeType* to IntPtr is not ambiguous (according
to standart)?

Example:

// System.IntPtr
class IntPtr
{
public static explicit System.IntPtr (int);
public static explicit System.IntPtr (long);
public static explicit System.IntPtr (void*);
}

struct SomeType {}

unsafe void f ( SomeType *p )
{
if ( (IntPtr)p == IntPtr.Zero ) // <-
{
// ...
}
}

Above example contain explicit conversion from SomeType* to IntPtr. The
question is how C# compiler select explicit conversion. Standart say: "A
user-defined explicit conversion consists of an optional standard explicit
conversion, followed by execution of a user-defined implicit or explicit
conversion operator, followed by another optional standard explicit
conversion." There are 3 user-defined exlicit conversions. Conversion
System.IntPtr (int) is removing from list because long is more encompassing
type than int (13.4.2). But neither of two remaining types is more
encompassing than other.

SomeType *p -> long -> IntPtr (explicit+user-defined explicit conversion)
SomeType *p -> void* -> IntPtr (implicit+user-defined explicit conversion)

If neither of two conversions is better than conversion is ambiguos. But
compiler think that conversion with through void* is better. Why? I not
found where standart say that implicit conversion is better than explicit
conversion.

Questions:
1. Is conversion from void* to IntPtr is processed as user-defined?
2. Why conversion sequence (System.IntPtr)(void*(p)) is better than
(System.IntPtr)(long(p))?

Any suggestions.

Alex.
Nov 16 '05 #1
2 6818

<snip>

SomeType *p -> long -> IntPtr (explicit+user-defined explicit conversion)
SomeType *p -> void* -> IntPtr (implicit+user-defined explicit conversion)

If neither of two conversions is better than conversion is ambiguos. But
compiler think that conversion with through void* is better. Why? I not
found where standart say that implicit conversion is better than explicit
conversion.

Questions:
1. Is conversion from void* to IntPtr is processed as user-defined?
I believe so 2. Why conversion sequence (System.IntPtr)(void*(p)) is better than
(System.IntPtr)(long(p))?


in section 25.4, a standard implicit conversion is defined for any
pointer-type to void*

"1 Finally, in an unsafe context, the set of standard implicit conversions
(§13.3.1) includes the following pointer conversion:
2 From any pointer-type to the type void*. "

and then section 13.4.2 says
" If a standard implicit conversion (§13.3.1) exists from a type A to a type
B, and if neither A nor B are interface-types, then A is said to be
encompassed by B, and B is said to encompass A. "

So, according to the spec, void* encompasses SomeType*. Conversions to long
and int are not standard implicit conversions and therefore would not
encompass SomeType*

Now, 13.4.4 says
"Otherwise, if any of the operators in U convert from types that encompass
S, then SX is the most encompassed type in the combined set of source types
of those operators. 10 If no most encompassed type can be found, then the
conversion is ambiguous and a compile-time error occurs. "

Out of the types int, long, and void*, only void* encompasses SomeType* and
therefore the set of types is reduced to strictly void* and therefore the
most encompassed type is void*. And because it is the most encompassed type,
SX becomes void* and when you follow the rules you mentioned above and no
explicit conversion occurs because there is a user defined conversion that
goes from SX to TX as defined, again, in 13.4.4:

"If U contains exactly one user-defined conversion operator that converts
from SX to TX, then this is the most specific conversion operator."

So, the best path is SomeType* -> void* -> IntPtr::explicit
IntPtr(void*) ->IntPtr
Nov 16 '05 #2
> Now, 13.4.4 says
"Otherwise, if any of the operators in U convert from types that encompass
S, then SX is the most encompassed type in the combined set of source types of those operators. 10 If no most encompassed type can be found, then the
conversion is ambiguous and a compile-time error occurs. "


I miss that. In other words - if standart implicit conversion from S to Sx
exist than compiler select best implicit conversion. Otherwise compiler
tryed to select some best standart explicit conversion.

Thank you Daniel!

Alex.
Nov 16 '05 #3

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

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
3
by: /* frank */ | last post by:
Explicit conversion is made by mean of a cast i.e. float a; int b; .... b = (int) a; But the implicit conversion? What is?
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
5
by: Andrey Simanovsky | last post by:
Consider the following code snippet: using System; enum E { A, } struct T { public static explicit operator T(int i) { return new T(); } public static void Foo() { E e = 0;
2
by: mark | last post by:
I have a function: Public Sub Inverse(ByVal Source_Matrix As Double(,), ByRef Destination_Matrix As Double(,)) Dim arrtemp(,) As Double = DirectCast(Source_Matrix, Double(,)) ........... For...
2
by: Carl Fenley | last post by:
Help a newbie out. I cannot seem to explicitly convert an Object to a Reflection.AssemblyCompanyAttribute type using DirectCast(). Turning Option Strict "Off" is not an option. Here is the...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
7
by: Fraser Ross | last post by:
I was looking at the new explicit conversion operators in the standard. The writing at the start of 12.3.2/2 doesn't make sense. "A conversion function may be explicit (7.1.2), in which case it is...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.