C++ Logo

std-proposals

Advanced search

Re: Using function type suggestion

From: Edward Diener <eldlistmailingz_at_[hidden]>
Date: Tue, 27 Oct 2020 15:51:43 -0400
On 10/27/2020 2:06 PM, Jason McKesson via Std-Proposals wrote:
> On Tue, Oct 27, 2020 at 2:03 PM Edward Diener via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
>>
>> On 10/27/2020 1:21 PM, Peter Sommerlad (C++) via Std-Proposals wrote:
>>> You can define aliases for function reference or function pointer types.
>>
>> What do you mean by a type alias for function reference as opposed to a
>> type alias for a function type ?
>
> He means this:
>
> ```
> using function_ref = int(&)(long, double, short, char);
> ```
>
> `function_ref` is a reference to a function. You can use it to declare
> reference variables to functions of that type.
>

Got it !

>>
>>>
>>> Why would you like to have an alias for a function type. Functions are
>>> neither objects nor references.
>>>
>>> You might be aware that template arguments can be formed from such
>>> "degenerated" function types (see std::function template arguments for
>>> example)
>>>
>>> So what is the problem you are intending to solve by your suggestion?
>>>
>>> IDEs tend to be quite good at keeping function signatures in sync.
>>
>> If I can declare a function using a type alias I think I should be able
>> to specify the function signature using the same alias when defining the
>> function.
>
> That doesn't answer the question: why do you *need* to do this?
>
> Equally importantly, you're going to have to type the parameter names
> again in order to use them in the function definition. So what's the
> point?

It seems to me purely a matter of syntactical convenience. I can declare
a function using the function type but in order to define that same
function I have to repeat all the types of that function type again,
rather than just be able to give a name to the function, give names to
the parameter types, and fill in the function body. Having to repeat the
return type of the function and all the parameter types, when that
information is already in the type alias for the function type, seems
needlessly repetitive to me.

I am also imagining this case: in template programming it is possible to
construct a function definition from some types passed to a template.
While it would be possible to construct a function definition based on
any combination of the individual types for the return type of a
function and the types of each of the function parameters, it would be
much more convenient if I could construct the function definition from a
function type itself.

>
>> For any data type I can use the same type alias when declaring
>> and defining the data type.
>
> Functions aren't data (object) types.

Obviously. But functions are still types in the C++ type system. The
fact that I can not treat them as types in the same way that I can treat
data types in the C++ type system is what this thread is all about.

Received on 2020-10-27 14:51:56