C++ Logo

std-proposals

Advanced search

[std-proposals] C++ feature ideea/proposal

From: PaulIRL <paulandreiabrudan_at_[hidden]>
Date: Tue, 05 Apr 2022 10:58:42 +0000
Hi, I'm currently learning C++ and at first I wondered why we can have 2 functions with the same name and different parameters but, not with the same name, same parameters and a different return type, but probably this is because the c++ compiler wouldn't know which function to call, however, I think that a good solution is forcing the user (programmer) to cast the output, and that way the compiler will know which function to call.Exampleint RandomNumber() { return 7; //for simplicity}
float RandomNumber() { return 5.2 //for simplicity again}
one could do
std::cout<<(float)RandomNumber()<<std::endl; //prints 5.2\n
howeverstd::cout<<RandomNumber()<<std::endl;
would throw a compiler error
this would also work as expectedstd::cout<<(int)(float)RandomNumber()<<std::endl; //prints 5\n

- Paul Abrudan
Trimis cu ProtonMail e-mail securizat.

Received on 2022-04-05 10:58:50