This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Named parameters


Hi,

In a discussion on comp.lang.c, the subject of "named parameters" (or
"designated parameters") has come up again.  This is a feature that some
of us feel would be very useful in C (and in C++).  I think it would be
possible to include it in the language without leading to any conflicts
with existing code - it is therefore something that could be made as a
gcc extension, with a hope of adding it to the standards for a later C
standards revision.

I wanted to ask opinions on the mailing list as to the feasibility of
the idea - there is little point in my cluttering up bugzilla with an
enhancement request if the gcc developers can spot obvious flaws in the
idea.


Basically, the idea is this:

int foo(int a, int b, int c);

void bar(void) {
	foo(1, 2, 3);	// Normal call
	foo(.a = 1, .b = 2, .c = 3)	// Same as foo(1, 2, 3)
	foo(.c = 3, .b = 2, .a = 1)	// Same as foo(1, 2, 3)
}

If only the first variant is allowed (with the named parameters in the
order declared in the prototype), then this would not affect code
generation at all - the designators could only be used for static error
checking.

If the second variant is allowed, then the parameters could be re-ordered.


The aim of this is to make it easier and safer to call functions with a
large number of parameters.  The syntax is chosen to match that of
designated initialisers - that should be clearer to the programmer, and
hopefully also make implementation easier.

If there is more than one declaration of the function, then the
designators used should follow the most recent in-scope declaration.


This feature could be particularly useful when combined with default
arguments in C++, as it would allow the programmer to override later
default arguments without specifying all earlier arguments.


At the moment, I am not asking for an implementation, or even /how/ it
might be implemented (perhaps a MELT plugin?) - I would merely like
opinions on whether it would be a useful and practical enhancement.


David


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]