Why doesn't this rise an error? What is happenning here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why doesn't this rise an error? What is happenning here?

struct A { A(int) {} }; void f(A) {} int main() { f(1); return 0; }

20th May 2017, 3:22 PM
Testing002
1 Answer
+ 1
When you call F(1), the compiler looks for a way to convert the arguments you have passed into the function into the proper type. Since you have given a constructor for A that takes an int, the compiler has a way to convert the int literal 1 into an object of type A. It constructs an object of type A using the value 1. This is similar to how you can pass an int to something that requires a double, but the compiler will upcast the int. https://code.sololearn.com/cvNW7YXzaOqz/?ref=app
25th May 2017, 2:33 PM
Matthew