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
42
votes
4 answers

Is there a way to avoid implicit conversion to void*?

I'm using an API that accepts void* in certain functions. I frequently accidentally pass the wrong pointer type to the function, and of course it compiles fine, but doesn't work at runtime. Is there a way to disable implicit conversion to void* for…
AILien
  • 832
  • 4
  • 11
42
votes
2 answers

Constexpr if with a non-bool condition

I seem to have found something that Clang and GCC disagree on. Here's the code: int main() { if constexpr (2) {} } This successfully compiles with GCC 7.4.0, but it fails with Clang 7.0.0 with this error message: test.cpp:3:17: error: constexpr…
41
votes
4 answers

How can I convert Scala Map to Java Map with scala.Float to java.Float k/v conversion

I would like to be able to perform the following, but it fails in the call to useMap. How can I perform this conversion? scala> import scala.collection.JavaConversions._ import scala.collection.JavaConversions._ scala> import…
37
votes
2 answers

Is it possible in Intellij IDEA Scala plugin to know which implicit conversion was applied?

When an implicit conversion is applied, IntelliJ underlines the converted code. Is it possible to navigate to the applied conversion in some way?
Diego
  • 5,024
  • 6
  • 38
  • 47
36
votes
1 answer

Intersection of multiple implicit conversions: reinventing the wheel?

Okay, fair warning: this is a follow-up to my ridiculous question from last week. Although I think this question isn't as ridiculous. Anyway, here goes: Previous ridiculous question: Assume I have some base trait T with subclasses A, B and C, I…
mergeconflict
  • 8,156
  • 34
  • 63
36
votes
1 answer

How do I easily convert from one collection type to another during a filter, map, flatMap in Scala?

Suppose I have a List[Int], and I want to call toString on each element, and get back the result as a Vector[String]. What are the various ways to do this in Scala? Is there a solution with a minimal amount of explicit typing? — i.e., I want to…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
35
votes
2 answers

Why is r-value reference constructor called in this case?

#include using namespace std; struct B{}; struct A { A(const B &) { cout<<"A(const B &)"<
Caesar
  • 971
  • 6
  • 13
34
votes
4 answers

How does implicit conversion work in Java?

I know that in Java Integer literals are int by default,so if I write something like this byte byteValue = 2; Java auto converts the literal value 2(which is an int by default) to byte. And the same thing works if I write byte byteValue =…
34
votes
4 answers

Does Swift support implicit conversion?

For example, I have the following code: let numberOfBlocks = 3 let blockWidth = SKSpriteNode(imageNamed: "image.png").size.width let padding = 20.0 let offsetX : Float = (self.frame.size.width - (blockWidth * numberOfBlocks +…
Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
33
votes
2 answers

What is the performance impact of Scala implicit type conversions?

In Scala, is there a significant CPU or memory impact to using implicit type conversions to augment a class's functionality vs. other possible implementation choices? For example, consider a silly String manipulation function. This implementation…
Chris Nauroth
  • 9,614
  • 1
  • 35
  • 39
33
votes
1 answer

What is the justification for this Nullable behavior with implicit conversion operators

I encountered some interesting behavior in the interaction between Nullable and implicit conversions. I found that providing an implicit conversion for a reference type from a value type it permits the Nullable type to be passed to a function…
Thomas
  • 3,603
  • 1
  • 18
  • 23
32
votes
1 answer

Why is this function call ambiguous?

I'm reading the standard and trying to figure out why this code won't be resolved without a cast. void foo(char c) { } // Way bigger than char void foo(unsigned long int) { } int main() { foo(123456789); // ambiguous foo((unsigned long int)…
32
votes
2 answers

Why implicitConversions is required for implicit defs but not classes?

As far as I understand, implicit conversions can result in potentially hard to understand code, or code suffering from other problems (perhaps even bugs?), which is why they require explicit enabling in order to be used in code without getting…
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
31
votes
2 answers

Why does calling Python's 'magic method' not do type conversion like it would for the corresponding operator?

When I subtract a float from an integer (e.g. 1-2.0), Python does implicit type conversion (I think). But when I call what I thought was the same operation using the magic method __sub__, it suddenly does not anymore. What am I missing here? When I…
31
votes
1 answer

Varying behavior for possible loss of precision

In Java, when you do int b = 0; b = b + 1.0; You get a possible loss of precision error. But why is it that if you do int b = 0; b += 1.0; There isn't any error?
szupie
  • 826
  • 10
  • 18