implicit-cast

Will the c# compiler perform multiple implicit conversions to get from one type to another?

邮差的信 提交于 2019-12-01 22:34:16
问题 Let's say you have yourself a class like the following: public sealed class StringToInt { private string _myString; private StringToInt(string value) { _myString = value; } public static implicit operator int(StringToInt obj) { return Convert.ToInt32(obj._myString); } public static implicit operator string(StringToInt obj) { return obj._myString; } public static implicit operator StringToInt(string obj) { return new StringToInt(obj); } public static implicit operator StringToInt(int obj) {

Will the c# compiler perform multiple implicit conversions to get from one type to another?

一曲冷凌霜 提交于 2019-12-01 18:45:00
Let's say you have yourself a class like the following: public sealed class StringToInt { private string _myString; private StringToInt(string value) { _myString = value; } public static implicit operator int(StringToInt obj) { return Convert.ToInt32(obj._myString); } public static implicit operator string(StringToInt obj) { return obj._myString; } public static implicit operator StringToInt(string obj) { return new StringToInt(obj); } public static implicit operator StringToInt(int obj) { return new StringToInt(obj.ToString()); } } Will you then be able to write code like the following:

Why can't I downcast pointer to members in template arguments?

廉价感情. 提交于 2019-12-01 18:33:23
问题 If I make a pointer-to-base-member, I can convert it to a pointer-to-derived-member usually, but not when used within a template like Buzz below, where the first template argument influences the second one. Am I fighting compiler bugs or does the standard really mandate this not work? struct Foo { int x; }; struct Bar : public Foo { }; template<class T, int T::* z> struct Buzz { }; static int Bar::* const workaround = &Foo::x; int main() { // This works. Downcasting of pointer to members in

Why can't I downcast pointer to members in template arguments?

こ雲淡風輕ζ 提交于 2019-12-01 18:03:09
If I make a pointer-to-base-member, I can convert it to a pointer-to-derived-member usually, but not when used within a template like Buzz below, where the first template argument influences the second one. Am I fighting compiler bugs or does the standard really mandate this not work? struct Foo { int x; }; struct Bar : public Foo { }; template<class T, int T::* z> struct Buzz { }; static int Bar::* const workaround = &Foo::x; int main() { // This works. Downcasting of pointer to members in general is fine. int Bar::* y = &Foo::x; // But this doesn't, at least in G++ 4.2 or Sun C++ 5.9. Why

Order of implicit conversions in c#

☆樱花仙子☆ 提交于 2019-12-01 15:49:17
What is the order of implicit conversions done in Console.WriteLine(x) when x is an object from user-defined class: class Vector { public int x = 12; public static implicit operator double(Vector v1) { return 3.14; } public static implicit operator int(Vector v1) { return 42; } public override string ToString() { return this.x.ToString(); } } static void Main(string[] args) { Vector v11 = new Vector(); Console.WriteLine(v11); } Why I get 42, and not 3.14 or "12"? Why I can not add an additional implicit conversion to string /there is Compiler error on the ambiguity between CW(int) and CW

Order of implicit conversions in c#

家住魔仙堡 提交于 2019-12-01 15:29:27
问题 What is the order of implicit conversions done in Console.WriteLine(x) when x is an object from user-defined class: class Vector { public int x = 12; public static implicit operator double(Vector v1) { return 3.14; } public static implicit operator int(Vector v1) { return 42; } public override string ToString() { return this.x.ToString(); } } static void Main(string[] args) { Vector v11 = new Vector(); Console.WriteLine(v11); } Why I get 42, and not 3.14 or "12"? Why I can not add an

Implicit version of IsAssignableFrom?

拈花ヽ惹草 提交于 2019-11-30 20:31:08
In my code using reflections i wrote if (f.FieldType.IsAssignableFrom("".GetType())) I have a class that has an implicit conversion to strings. However the if statement above doesnt catch it. How can i make reflection/the above if statement catch strings and classes with implicit string conversion? instead of specifically strings and each class i know about? I would use an extension method which gets all public static methods and checks for a method with the correct name and return type. public static class TypeExtentions { public static bool ImplicitlyConvertsTo(this Type type, Type

Selectively disable subsumption in Scala? (correctly type List.contains)

最后都变了- 提交于 2019-11-30 07:21:28
问题 List("a").contains(5) Because an Int can never be contained in a list of String , this should generate an error at compile-time , but it does not. It wastefully and silently tests every String contained in the list for equality to 5 , which can never be true ( "5" never equals 5 in Scala). This has been named "the 'contains' problem". And some have implied that if a type system cannot correctly type such semantics, then why go through the extra effort for enforcing types. So I consider it is

Implicit version of IsAssignableFrom?

限于喜欢 提交于 2019-11-30 05:09:11
问题 In my code using reflections i wrote if (f.FieldType.IsAssignableFrom("".GetType())) I have a class that has an implicit conversion to strings. However the if statement above doesnt catch it. How can i make reflection/the above if statement catch strings and classes with implicit string conversion? instead of specifically strings and each class i know about? 回答1: I would use an extension method which gets all public static methods and checks for a method with the correct name and return type.

Selectively disable subsumption in Scala? (correctly type List.contains)

这一生的挚爱 提交于 2019-11-29 03:01:33
List("a").contains(5) Because an Int can never be contained in a list of String , this should generate an error at compile-time , but it does not. It wastefully and silently tests every String contained in the list for equality to 5 , which can never be true ( "5" never equals 5 in Scala). This has been named " the 'contains' problem ". And some have implied that if a type system cannot correctly type such semantics, then why go through the extra effort for enforcing types. So I consider it is an important problem to solve. The type parametrization B >: A of List.contains inputs any type that