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
66
votes
3 answers

Double pointer const-correctness warnings in C

A pointer to non-const data can be implicitly converted to a pointer to const data of the same type: int *x = NULL; int const *y = x; Adding additional const qualifiers to match the additional indirection should logically work the same…
66
votes
1 answer

implicit operator using interfaces

I have a generic class that I'm trying to implement implicit type casting for. While it mostly works, it won't work for interface casting. Upon further investigation, I found that there is a compiler error: "User-defined conversion from interface"…
64
votes
3 answers

mysql datetime comparison

For example the following query works fine: SELECT * FROM quotes WHERE expires_at <= '2010-10-15 10:00:00'; But this is obviously performing a 'string' comparison - I was wondering if there was a function built in to MySQL that specifically…
MAX POWER
  • 5,213
  • 15
  • 89
  • 141
61
votes
3 answers

Conditional operator cannot cast implicitly?

I'm a little stumped by this little C# quirk: Given variables: Boolean aBoolValue; Byte aByteValue; The following compiles: if (aBoolValue) aByteValue = 1; else aByteValue = 0; But this will not: aByteValue = aBoolValue ? 1 : 0; Error…
MPelletier
  • 16,256
  • 15
  • 86
  • 137
60
votes
2 answers

Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal

I think this question will bring me instant fame here on Stack Overflow. Suppose you have the following type: // represents a decimal number with at most two decimal places after the period struct NumberFixedPoint2 { decimal number; // an…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
60
votes
8 answers

Can you use keyword explicit to prevent automatic conversion of method parameters?

I know you can use C++ keyword 'explicit' for constructors of classes to prevent an automatic conversion of type. Can you use this same command to prevent the conversion of parameters for a class method? I have two class members, one which takes a…
Superpolock
  • 3,515
  • 7
  • 30
  • 24
60
votes
2 answers

Should implicit classes always extend AnyVal?

Say I'm writing an extension method implicit class EnhancedFoo(foo: Foo) { def bar() { /* ... */ } } Should you always include extends AnyVal in the class defininition? Under what circumstances would you not want to make an implicit class a value…
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
59
votes
5 answers

Why does a generic type constraint result in a no implicit reference conversion error?

I have created a couple of interfaces and generic classes for working with agenda appointments: interface IAppointment where T : IAppointmentProperties { T Properties { get; set; } } interface IAppointmentEntry where T :…
Rens
  • 592
  • 1
  • 4
  • 7
57
votes
1 answer

Concatenate two Dictionaries

Given some Dictionaries Dictionary GroupNames = new Dictionary(); Dictionary AddedGroupNames = new Dictionary(); I am unable to merge them into one: GroupNames =…
Alexander
  • 19,906
  • 19
  • 75
  • 162
56
votes
5 answers

Ternary operator implicit cast to base class

Consider this piece of code: struct Base { int x; }; struct Bar : Base { int y; }; struct Foo : Base { int z; }; Bar* bar = new Bar; Foo* foo = new Foo; Base* returnBase() { Base* obj = !bar ? foo : bar; return obj; } int…
53
votes
3 answers

Why does an implicit conversion operator from to accept ?

This is a weird behaviour that I cannot make sense of. In my example I have a class Sample and an implicit conversion operator from T to Sample. private class Sample { public readonly T Value; public Sample(T value) { Value…
Noel Widmer
  • 4,444
  • 9
  • 45
  • 69
50
votes
4 answers

Nonintuitive result of the assignment of a double precision number to an int variable in C

Could someone give me an explanation why I get two different numbers, resp. 14 and 15, as an output from the following code? #include int main() { double Vmax = 2.9; double Vmin = 1.4; double step = 0.1; double a…
GeorgiD
  • 523
  • 4
  • 6
50
votes
3 answers

When does lvalue-to-rvalue conversion happen, how does it work, and can it fail?

I see the term "lvalue-to-rvalue conversion" used in many places throughout the C++ standard. This kind of conversion is often done implicitly, as far as I can tell. One unexpected (to me) feature of the phrasing from the standard is that they…
orm
  • 2,835
  • 2
  • 22
  • 35
43
votes
4 answers

Why is signed and unsigned addition converted differently for 16 and 32 bit integers?

It seems the GCC and Clang interpret addition between a signed and unsigned integers differently, depending on their size. Why is this, and is the conversion consistent on all compilers and platforms? Take this example: #include #include…
Nikolaj
  • 1,137
  • 10
  • 22
43
votes
3 answers

Passing int as bool argument in C++

could someone explain what happens in this code: example here: https://ideone.com/1cFb4N #include using namespace std; class toto { public: bool b; toto(bool x) { cout<< "constructor bool:" << (x ? "true":…
serge
  • 13,940
  • 35
  • 121
  • 205