Questions tagged [implicit-conversion]

Converting an object, variable or value from one type to another to satisfy a type restriction, without specifically requesting that conversion through language syntax.

Converting an object, variable or value from one type to another to satisfy a type restriction, without specifically requesting that conversion through language syntax.

Implicit conversion is a subset of .

2401 questions
21
votes
6 answers

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

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…
Shelby Moore III
  • 6,063
  • 1
  • 33
  • 36
21
votes
2 answers

When should you use direct initialization and when copy initialization?

Is it simply preference or are there specific instances where one is necessary over another? I'm refering to the following variants for initialization T t(e); // direct initialization T t = e; // copy initialization
21
votes
1 answer

Option getOrElse type mismatch error

Why does this code raise a type mismatch error in Scala 2.9.2? I expected that getOrElse returns type String but actually it returns java.io.Serializable: scala> implicit def StringToOption(s:String) = Option(s) StringToOption: (s:…
sndyuk
  • 2,720
  • 2
  • 24
  • 32
21
votes
3 answers

How does SQL Server decide format for implicit datetime conversion?

declare @str_datetime varchar(50) set @str_datetime='30-04-2012 19:01:45' -- 30th April 2012 declare @dt_datetime datetime select @dt_datetime=@str_datetime This is giving following error: Msg 242, Level 16, State 3, Line 4 The conversion of a…
Shwet Jain
  • 213
  • 1
  • 2
  • 4
20
votes
6 answers

Warnings or errors for C++ implicit conversion of primitives

I've done some heavy refactoring of some C++ code, and discovered numerous bugs arising from implicit conversions that I'm not aware of. Example struct A *a(); bool b() { return a(); } void c() { int64_t const d(b()); } Issues In b, the…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
20
votes
1 answer

Strange implicit conversions with the ternary operator

I have the following code: class A { public: operator int() const { return 5; } }; class B { public: operator int() const { return 6; } }; int main() { A a; B b; int myInt = true ? a : b; return 0; } Attempting to compile…
20
votes
3 answers

Why does the Linq Cast<> helper not work with the implicit cast operator?

Please read to the end before deciding of voting as duplicate... I have a type that implements an implicit cast operator to another type: class A { private B b; public static implicit operator B(A a) { return a.b; } } class B { } Now,…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
20
votes
4 answers

How can I get implicit conversions to work inside collections?

Say I have an implicit conversion: implicit def aToB(a: A):B={ ... } How can I get this implicit conversion to work on the elements of a List? If I have: val listOfA: List[A] ... and I have a function that takes a List of B, is it possible to let…
Jack
  • 16,506
  • 19
  • 100
  • 167
19
votes
3 answers

Should this compile? Overload resolution and implicit conversions

This example seems to compile with VC10 and gcc (though my version of gcc is very old). EDIT: R. Martinho Fernandez tried this on gcc 4.7 and the behaviour is still the same. struct Base { operator double() const { return 0.0; } }; struct…
ryaner
  • 3,927
  • 4
  • 20
  • 23
19
votes
2 answers

Why does an explicit cast to ‘decimal’ call an explicit operator to ‘long’?

Consider the following code: class Program { public static explicit operator long(Program x) { return 47; } static int Main(string[] args) { var x = new Program(); Console.WriteLine((decimal) x); } } To my surprise,…
Timwi
  • 65,159
  • 33
  • 165
  • 230
19
votes
6 answers

Copy initialization with deleted copy constructor in reference initialization

Consider the follow code: #include class Data{ public: Data() = default; Data(Data const&) = delete; Data(int) { } }; int main(){ int a = 0; const std::string& rs = "abc"; // rs refers to temporary copy-initialized…
xmh0511
  • 7,010
  • 1
  • 9
  • 36
19
votes
3 answers

User-defined conversions sequence

Before I studied the explicit keyword, my teacher said: "compiler doesn't execute consecutive user defined conversion". If it is true, are there any errors in my code? Or have I misunderstood my teacher? I'm working in…
User8500049
  • 410
  • 3
  • 12
19
votes
1 answer

Can/should I use implicit operator instead of overriding ToString?

I have a class that I want to easily write out to strings (e.g. for logging purposes). Can I use the implicit operator to implicitly cast the object to a string rather than overriding the ToString method? For example, I have a Person class with Name…
Adam Smith
  • 659
  • 6
  • 15
19
votes
2 answers

implicit conversion from class to enumeration type in switch conditional

g++ 4.9.0 accepts the following code: enum E { foo }; struct C { operator E() const { return foo; } operator E() { return foo; } }; int main() { C c; switch (c) { case foo: break; } } But clang 3.4.1 rejects it with the following…
Oktalist
  • 14,336
  • 3
  • 43
  • 63
19
votes
1 answer

When is explicit move needed for a return statement?

In a comment to another question Jonathan Wakely responds to my statement: You never need explicit move for a local variable function return value. It's implicit move there -> ... never say never ... You need an explicit move if the local…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337