C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 24 Jan 2023 22:46:45 +0000
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'.

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;
}

Received on 2023-01-24 22:46:58