C++ Logo


Advanced search

Re: [std-proposals] Return type of function available inside function

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Tue, 24 Jan 2023 18:54:24 -0500
On Tue, Jan 24, 2023 at 5:47 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
> On Tue, Jan 24, 2023 at 10:25 PM Jason McKesson via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> > Because the language says that it is.
> >
> > The member access works because all member functions defined in the
> > class definition are effectively placed after the end of that class
> > definition. Therefore, they get to access class members that were not
> > yet defined.
> >
> > Referring to things in a function that haven't been defined yet isn't allowed.
> I feel like I'm pointing at water here and saying "water is wet" but
> anyway . . . I'll spell it out:
> 'return_t' can be determined from the return statement, even if the
> return statement comes after the first use of 'return_t'.

That would require the compiler to continue compiling the rest of the
code while having absolutely no idea what `return_t` even means. It
therefore needs to somehow make sense of code like this:

auto func()
  return_t a = 10;
  a = a + 5;
  return 5;

The validity of those two statements depends entirely on the third.
But C++ compilation doesn't work that way; later statements cannot
define what earlier statements mean.

The member function definition trick works by delaying compilation of
the member function definition *in its entirety* until the end of the
class. You can't delay *part of* compilation until later.

Received on 2023-01-24 23:54:55