473,757 Members | 8,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6867

<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::explici t
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
5654
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 at the bottom. Hope you can help me understand the "explicit" keyword and its usage. Specifically, Is "explicit" keyword only associated with constructor in C++? What's "implied conversion of types"?
9
2373
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 people to be able to initialize real numbers (like an object of type double) from it ... in such a context (or if someone explicitly casts to double), the imaginary part is to be ignored. However, I certainly do not want people to be able to ask...
3
5504
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
7622
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 Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the implicit conversion operator. Why does this happen? Is there any way that I can keep the implicit...
5
3234
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
1054
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 i = 1 To n For j = n + 1 To 2 * n arrtemp(i, j - n) = arrInv(i, j)
2
1412
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 Error: Option Strict On disallows implicit conversions from 'System.Object' to 'System.Reflection.AssemblyCompanyAttribute'. Here is the code:
4
2022
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 functions that offer conversions instead of conversion operators. In page 87, example 2: Errors that work. class String { // ...
7
2187
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 only considered as a user-defined conversion for direct-initialization (8.5)." That sentence is ok. "Otherwise, user-defined conversions are not restricted to use in assignments and initializations." This one contradicts the other. ...
0
9489
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
9298
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
10072
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
9906
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
9885
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
9737
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7286
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
6562
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();...
3
2698
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.