Generic selection (C11)

A generic selection is a primary expression. Its type and value depend on the selected generic association.

The following diagram shows the generic selection syntax:

Read syntax diagramSkip visual syntax diagram
                                          .-,-----------------------------------------.      
                                          V                                           |      
>>-_Generic--(--assignment-expression--,----+-type-name--:--assignment-expression---+-+--)-><
                                            |                                   (1) |        
                                            '-default--:--assignment-expression-----'        

Notes:
  1. A generic selection can have at most one default generic association.
where:
type-name
Specifies the type of a generic association. The type name that you specify in a generic association must be a complete object type other than a variably modified type.
assignment-expression
Is an assignment expression. The first assignment expression is called the controlling expression.
The generic association list is a group of generic associations. There are two forms of generic associations:
  • type-name: assignment-expression
  • default: assignment-expression

One generic selection cannot have two or more generic associations that specify compatible types. In one generic selection, the controlling expression can have at most one compatible type name in the generic association list. If a generic selection has no default generic association, its controlling expression must have exactly one compatible type name in its generic association list.

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

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

Example

The following sample myprogram.c defines a type-generic macro:
#define myfunction(X) _Generic((X), \
long double:myfunction_longdouble, \
default:myfunction_double, \
float:myfunction_float \
)(X)
void myfunction_longdouble(long double x){printf("calling %s\n",__func__);}
void myfunction_double(double x){printf("calling %s\n",__func__);} 
void myfunction_float(float x){printf("calling %s\n",__func__);}

int main()
{
  long double ld;
  double d;
  float f;
  myfunction(ld);
  myfunction(d);
  myfunction(f);
} 
When you execute the program:
xlc myprogram.c -qldbl128 -qlanglvl=extc1x
./a.out
the result is as follows:
calling myfunction_longdouble
calling myfunction_double
calling myfunction_float


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us