C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Return type deduction

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Wed, 9 Mar 2022 15:14:47 +0100
Il 09/03/22 14:46, Baruch Burstein via Std-Proposals ha scritto:
> Under the current rules, if a function uses return-type deduction
> (a.k.a. auto return), all the return statements must deduce to the
> same type. I want to suggest to relax this rule, to match the rules
> for determining the result type of the ternary operator.

To me there's a drastic difference between the ternary operator and what
you're proposing, which is the "locality" of the required reasoning.

A ternary operator is local. Whether you like or not the fact that it
tries to find a common type, you can see the two sub-expressions
involved, right next to each other.


Multiple return statements can instead appear at any point in a
function. I don't like very much the idea that in something like

auto f(bool b)
{
   if (b) return 1;

   // ... a thousand lines later ...

   return 3.14;
}

the multiple returns make the function return double, not int. In
general, I don't like that I've to inspect all the `return` statements
(non-local reasoning) to understand what the return type could possibly be.

This could easily open the door to all sorts of bugs, because a change
done at any point in a function will affect its return type:


auto f(bool b)
{
   if (b) return "hello"s;

   // ... a thousand lines later ...

   return "world"sv; // whops, meant s
}


My 2 c,
-- 
Giuseppe D'Angelo

Received on 2022-03-09 14:14:51