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
19
votes
1 answer

Indexing an std::vector with a negative index

I tried to index a vector using a negative index. The vector::at() member function checks whether the specified index is within the bounds of the vector, and if this does not occur, an out_of_range exception is thrown. vector array; //…
enzom83
  • 8,080
  • 10
  • 68
  • 114
19
votes
1 answer

passing const pointer by reference

I am confused that why following code is not able to compile int foo(const float* &a) { return 0; } int main() { float* a; foo(a); return 0; } Compiler give error as: error: invalid initialization of reference of type 'const…
ravi
  • 3,304
  • 6
  • 25
  • 27
18
votes
2 answers

Why can template instances not be deduced in `std::reference_wrapper`s?

Suppose I have some object of type T, and I want to put it into a reference wrapper: int a = 5, b = 7; std::reference_wrapper p(a), q(b); // or "auto p = std::ref(a)" Now I can readily say if (p < q), because the reference wrapper has a…
18
votes
1 answer

Spark Implicit $ for DataFrame

Where in the sqlContext.implicits._ does it define the $"string" to represent a dataframe call to the parent dataframe's column? Specifically I was confused on seeing something like the following: import…
nobody
  • 7,803
  • 11
  • 56
  • 91
18
votes
5 answers

C++, does bool conversion always fall back to implicit conversion to void*?

Question: Does implicit bool conversions always fall back to attempting implicit conversion to void*? (If such a conversion function exists for the type). If so, why? Consider the following short program: #include class Foo{ public: …
jensa
  • 2,792
  • 2
  • 21
  • 36
18
votes
3 answers

c = a + b and implicit conversion

With my compiler, c is 54464 (16 bits truncated) and d is 10176. But with gcc, c is 120000 and d is 600000. What is the true behavior? Is the behavior undefined? Or is my compiler false? unsigned short a = 60000; unsigned short b = 60000; unsigned…
VTiTux
  • 311
  • 1
  • 11
18
votes
5 answers

Implicit conversion to Runnable?

As an exercise, I tried to create an implicit conversion that would accept a function and produce a Runnable. That way you could call Java methods that accept Runnable objects and use them like closures. The implicit conversion is easy enough: …
Patrick Arnesen
  • 1,138
  • 1
  • 9
  • 13
18
votes
2 answers

Issue with std::reference_wrapper

The issue is clear with the following code: #include #include #include int main() { //std::vector a, b; int a = 0, b = 0; auto refa = std::ref(a); auto refb = std::ref(b); std::cout << (refa < refb)…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
18
votes
1 answer

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 …
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
18
votes
1 answer

Eta-expansion between methods and functions with overloaded methods in Scala

I would like to understand why the eta-expansion (§6.26.5) does not work for overloaded methods. For example, if I have following two methods: def d1(a: Int, b: Int) {} def r[A, B](delegate: (A, B) ⇒ Unit) {} I can do this: r(d1) But, when…
fikovnik
  • 3,473
  • 3
  • 28
  • 29
17
votes
1 answer

Why this type of implicit conversion is illegal?

I write the following implicit conversion in scala: implicit def strToInt2(str: String):Int = { str.toInt } But it rises this compilation error: :9: error: type mismatch; found : str.type (with underlying type String) required:…
zjffdu
  • 25,496
  • 45
  • 109
  • 159
17
votes
3 answers

How many implicits are there in Scala?

If I haven't imported anything but Scala's usual defaults, how many implicits (implicit conversions) are in scope? Is there a complete list of them somewhere, preferably organized by type that they could act upon?
kittylyst
  • 5,640
  • 2
  • 23
  • 36
17
votes
1 answer

Correct syntax to store function pointer

Surprisingly, the following code compiles well both in gcc and clang no matter what symbol before function name is used: *, & or nothing. Does standard allow any of them? What is preferred way to store function pointers? #include typedef…
user8044236
17
votes
5 answers

Comparison of bool data types in C++

The bool data type is commonly represented as 0 (as false) and 1 (as true). However, some say that true values can be represented by a value other than 1. If the later statement is true, then the following expression may be incorrect. bool x = 1; if…
rezabakhsh
  • 301
  • 2
  • 5
17
votes
2 answers

Implicit conversion and operator overload

So, I wrote something like this #include using namespace std; void f(int32_t i) { cout << "int32: " << i << endl; } void f(int16_t i) { cout << "int16: " << i << endl; } void f(int8_t i) { cout << "int8: " << i <<…
Eternal
  • 2,648
  • 2
  • 15
  • 21