Date: Tue, 5 Apr 2022 12:00:07 +0100
You can emulate that by returning a type with two conversion operators to
the types you want.
On Tue, Apr 5, 2022 at 11:59 AM PaulIRL via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> 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.
> Example
> int 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
>
> however
> std::cout<<RandomNumber()<<std::endl;
> would throw a compiler error
>
> this would also work as expected
> std::cout<<(int)(float)RandomNumber()<<std::endl; //prints 5\n
>
> - Paul Abrudan
>
> Trimis cu ProtonMail <https://protonmail.com/> e-mail securizat.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
the types you want.
On Tue, Apr 5, 2022 at 11:59 AM PaulIRL via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> 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.
> Example
> int 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
>
> however
> std::cout<<RandomNumber()<<std::endl;
> would throw a compiler error
>
> this would also work as expected
> std::cout<<(int)(float)RandomNumber()<<std::endl; //prints 5\n
>
> - Paul Abrudan
>
> Trimis cu ProtonMail <https://protonmail.com/> e-mail securizat.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2022-04-05 11:00:19