C++ Logo


Advanced search

Re: [std-proposals] Return type deduction

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Thu, 10 Mar 2022 01:40:12 -0500
On Wed, Mar 9, 2022 at 1:25 PM Zhihao Yuan via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> On Wednesday, March 9th, 2022 at 6:45 AM, Arthur O'Dwyer via Std-Proposals <std-proposals_at_[hidden]> wrote:
> The way I thought they worked was: The lexically first return is the one that counts. Every other return will implicitly convert to the first-deduced type.
> auto f(int *p) { if (cond) return p; return nullptr; } // Proposed: OK, return int*
> auto f(int *p) { if (cond) return nullptr; return p; } // Proposed: Still error, cannot convert int* to deduced return type std::nullptr_t
> This may cause some funny interactions
> with conditional returns in constexpr if
> statements.
> [...] The current rule — "every return must exactly match the lexically first one" — follows WG21's general principle of doing the most conservative thing possible in order to leave these doors open.
> I would prefer having some mechanism to
> tell the language "this type never contributes
> to return type deduction (unless it is the
> only choice, if you want to prevent breaking
> existing code)"

No, if we're going to do that, it should be something that's a part of
the return statement itself, not something in another file somewhere.

Received on 2022-03-10 06:40:11