Doc No: SC22/WG14/N1441
Date: 2010-03-09
Reply to: Clark Nelson <clark.nelson@intel.com>

General support for type-generic macros

This paper contains proposed wording to specify the _Generic construct proposed in N1404. New text is indented, and underlined when it doesn't come in whole paragraphs.

In 6.4.1p1, add a new keyword to the table:

_Generic

In 6.5.1p1, add a new alternative to the production for primary-expression:

primary-expression:
identifier
constant
string-literal
( expression )
generic-selection

Add a new subsection of 6.5.1:

6.5.1.1 Generic selection

Syntax

Paragraph 1:

generic-selection:
_Generic ( assignment-expression , generic-association-list )
generic-association-list:
generic-association
generic-association-list , generic-association
generic-association:
type-name : assignment-expression
default : assignment-expression

Constraints

Paragraph 2:

A generic selection shall have no more than one default generic association. The type name in a generic association shall specify an object type other than a variably modified type. The controlling expression of a generic selection shall have type compatible with at most one of the types named in its generic association list. If a generic selection has no default generic association, its controlling expression shall have type compatible with exactly one of the types named in its generic association list.

Note that there is no requirement that the types named in generic associations are all distinct. This would require a quadratic algorithm to test; it hardly seems worth the effort, since otherwise only linear processing is needed.

Variably modified types are disallowed because (A) that eliminates the possibility that a variable dimension in an array declarator might have to be evaluated, and (B) given the necessarily local nature of variably modified types, it's not clear to me how they could be useful in generic function selection.

Semantics

Paragraph 3:

The controlling expression of a generic selection is not evaluated. If a generic selection has a generic association with a type name that is compatible with the type of the controlling expression, then the result expression of the generic selection is the expression in that generic association. Otherwise, the result expression of the generic selection is the expression in the default generic association. None of the expressions from any other generic association of the generic selection is evaluated.

Note that no promotion is performed on the controlling expression. Effectively throwing away type information about the controlling expression would seem counterproductive in a language feature that's intended for type analysis.

Paragraph 4:

The type and and value of a generic selection are identical to those of its result expression. It is an lvalue, a function designator, or a void expression if its result expression is, respectively, an lvalue, a function designator, or a void expression.

Paragraph 5:

EXAMPLE

#define cbrt(X) _Generic(X, long double: cbrtl, default: cbrt, float: cbrtf)(X)