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

More on implicit conversion operators and interfaces in C# (again)

Okay. I've read this post, and I'm confused on how it applies to my example (below). class Foo { public static implicit operator Foo(IFooCompatible fooLike) { return fooLike.ToFoo(); } } interface IFooCompatible { Foo…
gregsdennis
  • 7,218
  • 3
  • 38
  • 71
30
votes
4 answers

What is the difference between double a = a + int b and int a += double b?

Why does: public class Addition { public static void main() { int a = 0; double b = 1.0; a = a + b; System.out.println(a); } } not compile but: public class Addition { public static void main() { int a = 0; …
rawnd
  • 375
  • 4
  • 8
30
votes
3 answers

How to prevent implicit conversion from int to unsigned int?

Suppose you have this: struct Foo { Foo(unsigned int x) : x(x) {} unsigned int x; }; int main() { Foo f = Foo(-1); // how to get a compiler error here? std::cout << f.x << std::endl; } Is it possible to prevent the implicit…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
29
votes
2 answers

Avoid or warn on implicit conversion from const char* to bool in GCC

Consider the following code: void foo(bool parameter) { std::cout << parameter << "\n"; } int main() { foo("const char *argument"); } I want the compiler to raise a warning when passing const char* instead of bool as a parameter to…
28
votes
1 answer

Odd bit shifting behavior

I have the following C code which works: int ex(unsigned int x) { int mask = 0x55555555; int a = ((x >> 0) & mask ); return a + ((x >> 1) & mask ); } However, when I expand it to this, I get a different result: int ex(unsigned int x)…
cdignam
  • 1,376
  • 1
  • 15
  • 21
27
votes
4 answers

How can I determine if an implicit cast exists in C#?

I have two types, T and U, and I want to know whether an implicit cast operator is defined from T to U. I'm aware of the existence of IsAssignableFrom, and this is not what I'm looking for, as it doesn't deal with implicit casts. A bit of googling…
Brann
  • 31,689
  • 32
  • 113
  • 162
27
votes
5 answers

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(object instance, string…
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
26
votes
2 answers

Can I add an implicit conversion for two classes which I don't directly control?

I'd like to be able to implicitly convert between two classes which are otherwise incompatible. One of the classes is Microsoft.Xna.Framework.Vector3, and the other is just a Vector class used in an F# project. I'm writing a 3d game in C# with XNA,…
Carson Myers
  • 37,678
  • 39
  • 126
  • 176
26
votes
2 answers

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)…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
26
votes
1 answer

In overload resolution, does selection of a function that uses the ambiguous conversion sequence necessarily result in the call being ill-formed?

The question arose while I was researching the answer to this SO question. Consider the following code: struct A{ operator char() const{ return 'a'; } operator int() const{ return 10; } }; struct B { void operator<< (int) { } }; int…
T.C.
  • 133,968
  • 17
  • 288
  • 421
26
votes
6 answers

Classes with both template and non-template conversion operators in the condition of switch statement

The problem originally arose in this question. Consider the following code: class Var { public: operator int () const { return 0; } template operator T () const { return T(); } }; int main() { Var v; …
T.C.
  • 133,968
  • 17
  • 288
  • 421
25
votes
4 answers

Implicit conversion not allowed on return

#include bool f() { std::optional opt; return opt; } Does not compile: 'return': cannot convert from 'std::optional' to 'bool' Consulting reference I would have thought to find an explanation, but I read it as it should be…
darune
  • 10,480
  • 2
  • 24
  • 62
25
votes
3 answers

Is nullptr falsy?

When used as a boolean expression or transformed into a boolean either explicitly or implicitly, is nullptr consistently false? Is this implementation defined or specified in the standard? I wrote some code to test, but am not certain if it tests…
25
votes
2 answers

Ambiguous implicit values

I've been thinking I understand scala implicits until recently faced strange problem. In my application I have several domain classes case class Foo(baz: String) case class Bar(baz: String) And a class that is able to construct domain object from…
lambdas
  • 3,990
  • 2
  • 29
  • 54
24
votes
3 answers

Why is the address of this volatile variable always at 1?

I wanted to inspect the address of my variable volatile int clock; cout << &clock; But it always says that x is at address 1. Am i doing something wrong??
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212