C++ Logo

std-proposals

Advanced search

Re: Using function type suggestion

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Tue, 27 Oct 2020 20:58:32 -0400
On Tue, Oct 27, 2020 at 3:52 PM Edward Diener via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> 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.

Code is read more often than it is written. At the end of the day, the
trivial duration of time lost to writing the declaration over again
will be made up for many times over by the number of people who won't
look at your function and ask, "wait, what type was that parameter
again?"

And all that assumes that you're even bothering with the
declaration/definition distinction to begin with. Lots of code gets
put in headers, where that usually is unnecessary. And modules can
make that sort of thing much less important.

> 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.

Can you give me a concrete example of when such a thing would be useful?

>
> >
> >> 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.

That's because functions aren't objects. Nor are they likely to become
so in the future. A pointer to function is an object, but the function
*itself* is not an object.

Functions even having a type is a byproduct of what has to happen in
order to make it possible to have a pointer to a function. A function
pointer needs to be a pointer, but it also needs a type to point to.
Hence, you need functions to be part of the type system.

But they aren't objects. Variables are supposed to be either objects
or references to objects. So it makes no sense to add some kind of
weird variable-like definition syntax for functions if they're going
to continue to not be objects.

Function definitions are not supposed to be like variable definitions.

Received on 2020-10-27 19:58:45