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
439
votes
2 answers

Where does Scala look for implicits?

An implicit question to newcomers to Scala seems to be: where does the compiler look for implicits? I mean implicit because the question never seems to get fully formed, as if there weren't words for it. :-) For example, where do the values for…
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
212
votes
5 answers

Static implicit operator

I recently found this code: public static implicit operator XElement(XmlBase xmlBase) { return xmlBase.Xml; } What does static implicit operator mean?
Bart
  • 4,830
  • 16
  • 48
  • 68
194
votes
4 answers

Objective-C implicit conversion loses integer precision 'NSUInteger' (aka 'unsigned long') to 'int' warning

I'm working through some exercises and have got a warning that states: Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'int' #import int main (int argc, const char * argv[]) { …
monkeyboy
  • 1,961
  • 2
  • 13
  • 7
150
votes
15 answers

Can we define implicit conversions of enums in c#?

Is it possible to define an implicit conversion of enums in c#? something that could achieve this? public enum MyEnum { one = 1, two = 2 } MyEnum number = MyEnum.one; long i = number; If not, why not?
Adam Naylor
  • 6,172
  • 10
  • 49
  • 69
146
votes
8 answers

Why can I pass 1 as a short, but not the int variable i?

Why does the first and second Write work but not the last? Is there a way I can allow all 3 of them and detect if it was 1, (int)1 or i passed in? And really why is one allowed but the last? The second being allowed but not the last really blows my…
user34537
133
votes
5 answers

Implicit type promotion rules

This post is meant to be used as a FAQ regarding implicit integer promotion in C, particularly implicit promotion caused by the usual arithmetic conversions and/or the integer promotions. Example 1) Why does this give a strange, large integer number…
Lundin
  • 195,001
  • 40
  • 254
  • 396
119
votes
5 answers

How does `is_base_of` work?

How does the following code work? typedef char (&yes)[1]; typedef char (&no)[2]; template struct Host { operator B*() const; operator D*(); }; template struct is_base_of { template
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
116
votes
8 answers

What is the meaning of "operator bool() const"

For example: operator bool() const { return col != 0; } col is an int. How does operator bool() const work?
Cheng
  • 1,169
  • 2
  • 8
  • 8
105
votes
3 answers

Prevent function taking const std::string& from accepting 0

Worth a thousand words: #include #include class SayWhat { public: SayWhat& operator[](const std::string& s) { std::cout << s << "\n"; return *this; } }; int main() { SayWhat ohNo; // ohNo[1];…
kabanus
  • 24,623
  • 6
  • 41
  • 74
96
votes
3 answers

Implicit conversion vs. type class

In Scala, we can use at least two methods to retrofit existing or new types. Suppose we want to express that something can be quantified using an Int. We can define the following trait. Implicit conversion trait Quantifiable{ def quantify: Int…
ziggystar
  • 28,410
  • 9
  • 72
  • 124
89
votes
10 answers

Why does printf("%f",0); give undefined behavior?

The statement printf("%f\n",0.0f); prints 0. However, the statement printf("%f\n",0); prints random values. I realize I'm exhibiting some kind of undefined behaviour, but I can't figure out why specifically. A floating point value in which all…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
84
votes
9 answers

How do I avoid implicit conversions on non-constructing functions?

How do I avoid implicit casting on non-constructing functions? I have a function that takes an integer as a parameter, but that function will also take characters, bools, and longs. I believe it does this by implicitly casting them. How can I avoid…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
78
votes
3 answers

How can I chain implicits in Scala?

The pimp-my-library pattern allows me to seemingly add a method to a class by making available an implicit conversion from that class to one that implements the method. Scala does not allow two such implicit conversions taking place, however, so I…
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
71
votes
8 answers

Why is C++ allowing me to assign a const char to a const char *?​!

To my astonishment, this compiles: const char* c_str() { static const char nullchar = '\0'; return nullchar; } and it introduced a bug in my code. Thankfully, I caught it. Is this intentional by C++, or a compiler bug? Is there a reason why…
user541686
  • 205,094
  • 128
  • 528
  • 886
68
votes
1 answer

When can I use explicit operator bool without a cast?

My class has an explicit conversion to bool: struct T { explicit operator bool() const { return true; } }; and I have an instance of it: T t; To assign it to a variable of type bool, I need to write a cast: bool b = static_cast(t); bool…
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
1
2 3
99 100