Date: Tue, 27 Oct 2020 12:42:52 -0400
C++ has a very good type system for creating types and using them in the
same way whether in declarations or definitions. But one area of using
types seems less than ideal to me. Let us suppose we create a function type:
using my_function_type = int ( long, double, short, char ) ;
We can now declare a function using that type, as in:
my_function_type my_function ;
and this is totally equivalent to:
int my_function ( long, double, short, char ) ;
but there is no way to use that same type in the definition of the
function 'my_function'. Instead we must spell out the type again
entirely, adding in the 'my_function' name and the names for the
function parameters, as in:
int my_function ( long a1, double a2, short a3, char a4 )
{
// function body using the parameter names and returning an 'int' etc.
}
Ideally we would like to reuse that function type and just add in the
name 'my_function' and the names for the parameters 'a1', 'a2', 'a3',
and 'a4' which we use in the function body.
Has anybody ever seen this as a limitation of C++, as I do ?
I am not proposing a syntax solution but just bringing up this issue, to
see if any other C++ programmers regard this as a weakness in the C++
type system that might be remedied by a proposal. I bring this up
because I can not think offhand of any other situation where a type in
C++ can not be used in the same way both in the declaration and
definition of a C++ entity. Perhaps I am not making a necessary
distinction between using C++ types with data variables and callables,
but if so I would still like to hear other opinions in this matter.
same way whether in declarations or definitions. But one area of using
types seems less than ideal to me. Let us suppose we create a function type:
using my_function_type = int ( long, double, short, char ) ;
We can now declare a function using that type, as in:
my_function_type my_function ;
and this is totally equivalent to:
int my_function ( long, double, short, char ) ;
but there is no way to use that same type in the definition of the
function 'my_function'. Instead we must spell out the type again
entirely, adding in the 'my_function' name and the names for the
function parameters, as in:
int my_function ( long a1, double a2, short a3, char a4 )
{
// function body using the parameter names and returning an 'int' etc.
}
Ideally we would like to reuse that function type and just add in the
name 'my_function' and the names for the parameters 'a1', 'a2', 'a3',
and 'a4' which we use in the function body.
Has anybody ever seen this as a limitation of C++, as I do ?
I am not proposing a syntax solution but just bringing up this issue, to
see if any other C++ programmers regard this as a weakness in the C++
type system that might be remedied by a proposal. I bring this up
because I can not think offhand of any other situation where a type in
C++ can not be used in the same way both in the declaration and
definition of a C++ entity. Perhaps I am not making a necessary
distinction between using C++ types with data variables and callables,
but if so I would still like to hear other opinions in this matter.
Received on 2020-10-27 11:43:03