From WikiChip
Editing c/generic selection

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.

Latest revision Your text
Line 1: Line 1:
{{c title|Generic Selection}}
+
The '''generic selection''' is an expression that was added to the [[C programming language]] in the [[C11]] standard to facilitates compile-time choices based on the type of an expression. This feature was added to standardize the compiler magic that was required for [[C99|C99's]] time-generic math ([[tgmath.h - C|<tgmath.h>]]).
The '''generic selection''' is an expression that was added to the [[C programming language]] in the [[C11]] standard to facilitates compile-time choices based on the type of an expression. This feature was added to standardize the compiler magic that was required for [[C99|C99's]] type-generic math ({{C|tgmath.h|<tgmath.h>}}).
 
  
 
== Overview ==
 
== Overview ==
[[C99]] introduced a new set of math macros called type-generic math which were provided in the {{C|tgmath.h|<tgmath.h>}} header. These macros expanded to their appropriate functions based on the type of the arguments that were provided. For example, the cos() macro would expand to cosl(), cosf(), or ccos() depending if the argument is a double, a long double, or a {{C|_Complex|complex}} double.
+
[[C99]] introduced a new set of math macros called type-generic math which were provided in the [[tgmath.h - C|<tgmath.h>]] header. These macros expanded to their appropriate functions based on the type of the arguments that were provided. For example, the cos() macro would expand to cosl(), cosf(), or ccos() depending if the argument is a double, a long double, or a [[_Complex - C|complex]] double.
  
 
The C99 standard made no attempt at explaining how the functionality must be accomplished making each compiler do its own thing. [[C11]] introduced the ''generic selection expression'' which attempts to provide a standard mechanism for performing compile-time choices based on a type of an expression.
 
The C99 standard made no attempt at explaining how the functionality must be accomplished making each compiler do its own thing. [[C11]] introduced the ''generic selection expression'' which attempts to provide a standard mechanism for performing compile-time choices based on a type of an expression.
  
 
== Syntax ==
 
== Syntax ==
The '''generic selection expression''' is implemented with a new '''_Generic''' keyword:
+
The '''generic selection expression''' is implemented with a new '''_Generic''' keyboard:
  
 
<source lang="c">_Generic(  control-expression , generic-assoc-list );</source>
 
<source lang="c">_Generic(  control-expression , generic-assoc-list );</source>
Line 43: Line 42:
  
 
== Uses in <tgmath.h> ==
 
== Uses in <tgmath.h> ==
The [[tgmath.h|<tgmath.h>]] header that was added in [[C99]] added support for type-generic math using some compiler magic unspecified by the standard. C11 now provides a standardized facility for achieving this magic. For example, one could implement the {{C|tgmath.h/cbrt|cbrt}} generic function that computes the cube-root like this:
+
The [[tgmath.h|<tgmath.h>]] header that was added in [[C99]] added support for type-generic math using some compiler magic unspecified by the standard. C11 now provides a standardized facility for achieving this magic. For example, one could implement the [[tgmath.h/cbrt - C|cbrt]] generic function that computes the cube-root like this:
  
 
<source lang="C">
 
<source lang="C">
#define cbrt(X)                       \
+
#define cbrt(X)
 
         _Generic((X),                \
 
         _Generic((X),                \
 
                   long double: cbrtl,  \
 
                   long double: cbrtl,  \
Line 54: Line 53:
 
</source>
 
</source>
  
Implementing some of the functions with multiple arguments becomes far more lengthly with a selection for each of the original cases. Consider the {{C|tgmath.h/pow|pow}} type-generic function. One could implement it as:
+
Implementing some of the functions with multiple arguments becomes far more lengthly with a selection for each of the original cases. Consider the [[tgmath.h/pow - C|pow]] type-generic function. One could implement it as:
  
 
<source lang="c">
 
<source lang="c">

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)