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

… why I get two different numbers … Aside from the usual float-point issues, the computation paths to b and c are arrived in different ways. c is calculated by first saving the value as double a. double a =(Vmax-Vmin)/step; int b = (Vmax-Vmin)/step; int c = a; C allows intermediate floating-point math to be … Read more

Why does an implicit conversion operator from to accept ?

You can take a look at how compiler lowers this code: int? a = 3; Sample<int> sampleA = a; into this: int? nullable = 3; int? nullable2 = nullable; Sample<int> sample = nullable2.HasValue ? ((Sample<int>)nullable2.GetValueOrDefault()) : null; Because Sample<int> is a class its instance can be assigned a null value and with such an implicit … Read more

Ternary operator implicit cast to base class

Quoting from C++ standard draft N4296, Section 5.16 Conditional operator, Paragraph 6.3: One or both of the second and third operands have pointer type; pointer conversions (4.10) and qualification conversions (4.4) are performed to bring them to their composite pointer type (Clause 5). The result is of the composite pointer type. Section 5 Expressions, Paragraph … Read more

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

No, you can’t use explicit, but you can use a templated function to catch the incorrect parameter types. With C++11, you can declare the templated function as deleted. Here is a simple example: #include <iostream> struct Thing { void Foo(int value) { std::cout << “Foo: value” << std::endl; } template <typename T> void Foo(T value) … Read more

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

Let’s simplify: interface IAnimal { … } interface ICage<T> where T : IAnimal { void Enclose(T animal); } class Tiger : IAnimal { … } class Fish : IAnimal { … } class Cage<T> : ICage<T> where T : IAnimal { … } ICage<IAnimal> cage = new Cage<Tiger>(); Your question is: why is the last … Read more

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

I’m just replying to the first part of the question to start with. (I suggest that the second part should be a separate question; it’s more likely to be a bug.) There’s only an explicit conversion from decimal to int, but that conversion is being implicitly called in your code. The conversion happens in this … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)