implicit-conversion

Ternary operator implicit cast to base class

霸气de小男生 提交于 2020-07-28 06:33:06
问题 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 main() { returnBase(); return 0; } This doesn't work under Clang or GCC, giving me : error: conditional expression between distinct pointer types ‘Foo*’ and ‘Bar*’ lacks a cast Base* obj = !bar ? foo : bar; Which means for it to compile I have to change the code to : Base*

why can in TypeScript a possible number value in an interface be converted to a not possible number value in a class implementation?

丶灬走出姿态 提交于 2020-07-03 07:15:44
问题 Today I ran into an unexpected TypeScript compiler behaviour. I'm wondering if it's a bug or a feature. Probably it will be the last one, but then I would like to know the rationale behind it. If I declare an interface method with a parameter that can be a string | number , and create a class that implements that interface, then the class method can make that parameter only string . This leads to a situation where the class implementation is not expecting a number, but the compiler allows

Why can't I implicitly convert a double to an int?

ε祈祈猫儿з 提交于 2020-07-03 07:02:17
问题 You can implicitly convert an int to a double: double x = 5; You can explicitly convert an int to a double: double x = (double) 5; You can explicitly convert a double to an int: int x = (int) 5.0; Why can't you implicitly convert a double to an int? : int x = 5.0; 回答1: The range of double is wider than int . That's why you need explicit cast. Because of the same reason you can't implicitly cast from long to int : long l = 234; int x = l; // error 回答2: Implicit casting is only available when

Why can't I implicitly convert a double to an int?

戏子无情 提交于 2020-07-03 07:02:03
问题 You can implicitly convert an int to a double: double x = 5; You can explicitly convert an int to a double: double x = (double) 5; You can explicitly convert a double to an int: int x = (int) 5.0; Why can't you implicitly convert a double to an int? : int x = 5.0; 回答1: The range of double is wider than int . That's why you need explicit cast. Because of the same reason you can't implicitly cast from long to int : long l = 234; int x = l; // error 回答2: Implicit casting is only available when

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

霸气de小男生 提交于 2020-05-10 06:51:33
问题 Could someone give me an explanation why I get two different numbers, resp. 14 and 15, as an output from the following code? #include <stdio.h> int main() { double Vmax = 2.9; double Vmin = 1.4; double step = 0.1; double a =(Vmax-Vmin)/step; int b = (Vmax-Vmin)/step; int c = a; printf("%d %d",b,c); // 14 15, why? return 0; } I expect to get 15 in both cases but it seems I'm missing some fundamentals of the language. I am not sure if it's relevant but I was doing the test in CodeBlocks.

MISRA 2012 violation - Type mismatch (Rules 10.1, 10.4)

岁酱吖の 提交于 2020-03-21 18:59:49
问题 I'm facing MISRA C 2012 violation that I can't understand. Following is the code: #define I2C_CCRH_FS ((uint8_t)0x80) #define I2C_CCRH_DUTY ((uint8_t)0x40) #define I2C_CCRH_CCR ((uint8_t)0x0F) typedef struct I2C_struct { volatile uint8_t CR1; volatile uint8_t CR2; volatile uint8_t CCRL; volatile uint8_t CCRH; } I2C_TypeDef; #define I2C_BaseAddress 0x5210 #define I2C ((I2C_TypeDef *) I2C_BaseAddress) I2C->CCRH &= ~(uint8_t)((I2C_CCRH_FS | I2C_CCRH_DUTY) | I2C_CCRH_CCR); In the previous code,

Return copy of case class from generic function without runtime cast

落花浮王杯 提交于 2020-02-26 18:24:32
问题 I want to get rid of a runtime cast to a generic ( asInstanceOf[A] ) without implicit conversions. This happens when I have a fairly clean data model consisting of case classes with a common trait and want to implement a generic algorithm on it. As an example the resulting algorithm should take a class of type A that is a subclass of the trait T and is supposed to return a copy of the concrete class A with some updated field. This is easy to achieve when I can simply add an abstract copy