C++ Logo


Advanced search

Re: [std-proposals] Return type deduction

From: Zhihao Yuan <zy_at_[hidden]>
Date: Wed, 09 Mar 2022 18:25:31 +0000
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 interactionswith conditional returns in constexpr ifstatements.

> [...] 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 totell the language "this type never contributesto return type deduction (unless it is theonly choice, if you want to prevent breakingexisting code)"

  struct nullptr_t { // some syntax = delete; };

  auto f(int *p) { if (cond) return p; return nullptr; } // OK, return int* auto f(int *p) { if (cond) return nullptr; return p; } // OK, return int*

--Zhihao Yuan, ID lichray

The best way to predict the future is to invent it.


Received on 2022-03-09 18:25:34