C++ Logo

std-proposals

Advanced search

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

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Tue, 24 Jan 2023 23:06:03 +0000
On Tue, 24 Jan 2023 at 22:47, 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'.
>

Uh huh. And what will the compiler do in this case?

auto f() {
   return std::bool_constant<not return_t::value>();
}


> So when the compiler sees the following:
>
> template<typename T, typename A>
> auto Func(T obj0, A obj1)
> {
> return_t obj;
> return obj0 + obj1;
> }
>
> It picks out the return statement: obj0 + obj1
>
> It wraps it in decltype: decltype(obj0 + obj1)
>
> and then it makes a new type: typedef decltype(obj0 + obj1) return_t;
>
> So here's what the compiler turns it into:
>
> template<typename T, typename A>
> auto Func(T obj0, A obj1)
> {
> typedef decltype(obj0 + obj1) return_t;
> return_t obj;
> return obj0 + obj1;
> }
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-01-24 23:06:19