implicit-conversion

c++ unable to find operator via implicit conversion?

When writing a class to act as a wrapper around a heap-allocated object, I encountered a problem with implicit type conversion that can be reduced to this simple example. In the code below the wrapper class manages a heap-allocated object and implicitly converts to a reference to that object. This allows the wrapper object to be passed ...

C++ operator overloading and implicit conversion

Hi, everyone! I have a class that encapsulates some arithmetic, let's say fixed point calculations. I like the idea of overloading arithmetic operators, so I write the following: class CFixed { CFixed( int ); CFixed( float ); }; CFixed operator* ( const CFixed& a, const CFixed& b ) { ... } It all works. I can write 3 * CFixe...

simulate C++ function template instantiation with implicit conversion

I already asked two questions related to what I'm trying to do (one resolved, one of which I will close soon). I know that C++ template instantiation does not allow any implicit conversions (see for example this comment), but I would like to simulate it. Suppose I have the following skeleton code: template <class T> struct Base_A{ ...

Invaid cast exception, even when I have an implicit cast operator defined (in asp mvc app)

I have an mvc model class created and one of the properties is of type 'MyObject'. It also has a System.ComponentModel.DataAnnotations.StringLength attribute on it. MyObject as implicit cast operators so it can essentially be used as a string: public static implicit operator string(MyObject o){...} public static implicit operator MyOb...

Problem with implicit conversion and null

I have this function public static implicit operator MyClass(string v) { return new MyClass(v); } and write var.myclass = null;. This calls the implicit operator and passes null as string, which causes havoc in my code (i use reflection and would not like to add a special case). How can i write myclass = null without causing the impli...

Using string constants in implicit conversion

Consider the following code: public class TextType { public TextType(String text) { underlyingString = text; } public static implicit operator String(TextType text) { return text.underlyingString; } private String underlyingString; } TextType text = new TextType("Something"); String str = text; //...

How to tell if Type A is implicitly convertible to Type B

Given Type a and Type b, how can I, at runtime, determine whether there's an implicit conversion from a to b? If that doesn't make sense, consider the following method: public PropertyInfo GetCompatibleProperty<T>(object instance, string propertyName) { var property = instance.GetType().GetProperty(propertyName); bool isCompatib...

why does a char + another char = a weird number

Here's the code snippet: public static void main (String[]arg) { char ca = 'a' ; char cb = 'b' ; System.out.println (ca + cb) ; } The output is: 195 Why is this the case? I would think that 'a' + 'b' would be either "ab" , "12" , or 3. Whats going on here? ...

scala 2.8 implict java collections conversions

I have problem with JavaConversions with 2.8 beta: import scala.collection.JavaConversions._ class Utils(dbFile : File, sep: String) extends IUtils { (...) def getFeatures() : java.util.List[String] = csv.attributes.toList } And then exception: [INFO] Utils.scala:20: error: type mismatch; [INFO] found : List[String] [IN...

Why does this implicit type conversion in C# fail?

Background: Let's assume I've got the following class: class Wrapped<T> : IDisposable { public Wrapped(T obj) { /* ... */ } public static implicit operator Wrapped<T>(T obj) { return new Wrapped<T>(obj); } public void Dispose() { /* ... */ } } As you can see, it provides an implicit type conversion ope...

Question about implicit operator overloading in c#

MyClass c = 10; Is there any way to make this code work? I know that through the implicit operator overloading you can get the opposite to work: int i = instanceOfMyClass; Thanks ...

Why does Option not extend the Iterable trait directly?

Option is implicitly convertible to an Iterable - but why does it not just just implement Iterable directly: def iterator = new Iterator[A] { var end = !isDefined def next() = { val n = if (end) throw new NoSuchElementException() else get end = true n } def hasNext = !end } EDIT: In fact it's even weider than that...

Scala traits and implicit conversion confusion

The following lines work when I enter them by hand on the Scala REPL (2.7.7): trait myTrait { override def toString = "something" } implicit def myTraitToString(input: myTrait): String = input.toString object myObject extends myTrait val s: String = myObject However, if I try to compile file with it I get the following error: [erro...

Using implicit conversion as a substitute for multiple inheritance in .NET

I have a situation where I would like to have objects of a certain type be able to be used as two different types. If one of the "base" types was an interface this wouldn't be an issue, but in my case it is preferable that they both be concrete types. I am considering adding copies of the methods and properties of one of the base types...

C# implicit conversions

Hello, I'm currently working on an application where I need to load data from an SQL database and then assign the retrieved values into the properties of an object. I'm doing this by using reflection since the property names and column names are the same. However, many of the properties are using a custom struct type that is basically a...

Is it guaranteed that new Integer(i) == i in Java?

Consider the following snippet: int i = 99999999; byte b = 99; short s = 9999; Integer ii = Integer.valueOf(9); // should be within cache System.out.println(new Integer(i) == i); // "true" System.out.println(new Integer(b) == b); // "true" System.out.println(new Integer(s) == s); // "true" System.out.pri...

Can someone explain me implicit conversions in Scala?

And more specifically how does the BigInt works for convert int to BigInt? In the source code it reads: ... implicit def int2bigInt(i: Int): BigInt = apply(i) ... How is this code invoked? I can understand how this other sample: "Date literals" works. In. val christmas = 24 Dec 2010 Defined by: implicit def dateLiterals(date:...

Polymorphism, Autoboxing, and Implicit Conversions

Would you consider autoboxing in Java to be a form of polymorphism? Put another way, do you think autoboxing extends the polymorphic capabilities of Java? What about implicit conversions in Scala? My opinion is that they are both examples of polymorphism. Both features allow values of different data types to be handled in a uniform m...

How `is_base_of` works?

Why the following code works? typedef char (&yes)[1]; typedef char (&no)[2]; template <typename B, typename D> struct Host { operator B*() const; operator D*(); }; template <typename B, typename D> struct is_base_of { template <typename T> static yes check(D*, T); static no check(B*, int); static const bool value = sizeo...

How do you make a Factory that can return derived types?

I have created a factory class called AlarmFactory as such... 1 class AlarmFactory 2 { 3 public static Alarm GetAlarm(AlarmTypes alarmType) //factory ensures that correct alarm is returned and right func pointer for trigger creator. 4 { 5 switch (alarmType) 6 { 7 case AlarmTypes....