Generic selection

Bạn đang quan tâm đến Generic selection phải không? Nào hãy cùng TTTD đón xem bài viết này ngay sau đây nhé, vì nó vô cùng thú vị và hay đấy!                        

Generic selection
C Language Headers Type support Program utilities Variadic function support Error handling Dynamic memory management Date and time utilities Strings library Algorithms Numerics Input/output support Localization support Atomic operations (C11) Thread support (C11) Technical Specifications [edit] C language Basic concepts Keywords Preprocessor Statements Expressions Initialization Declarations Functions Miscellaneous History of C Technical Specifications [edit] Expressions General value category evaluation order and sequence points constant expressions implicit conversions generic selection constants and literals integer constant floating constant character constant string literal compound literal operators operator precedence member access and indirection logical operators comparison operators arithmetic operators assignment operators increment and decrement function call, comma, conditional operator sizeof _Alignof(C11) cast operators [edit]

Provides a way to choose one of several expressions at compile time, based on a type of a controlling expression


  • 1 Syntax
  • 2 Explanation
  • 3 Notes
  • 4 Keywords
  • 5 Example
  • 6 Defect reports
  • 7 References
  • 8 See also

[edit] Syntax

_Generic ( controlling-expression , association-list ) (since C11)

where association-list is a comma-separated list of associations, each of which has the syntax

type-name : expression default : expression


type-name – any complete object type that isn’t variably-modified (that is, not VLA or pointer to VLA). controlling-expression – any expression (except for the comma operator) whose type must be compatible with one of the type-names if the default association is not used expression – any expression (except for the comma operator) of any type and value category

No two type-names in the association-list may specify compatible types. There may be only one association that uses the keyword default. If default is not used and none of the type-names are compatible with the type of the controlling expression, the program will not compile.

XEM THÊM:  Công Dụng Tuyệt Vời Của Giấm Gạo Và Cách Làm Giấm Gạo

[edit] Explanation

Xem thêm: Tử vi trọn đời tuổi Tân Sửu 2021 nam mạng chi tiết nhất –

First, the type of controlling-expression undergoes lvalue conversions. The conversion is performed in type domain only: it discards the top-level cvr-qualifiers and atomicity and applies array-to-pointer/function-to-pointer transformations to the type of the controlling expression, without initiating any side-effects or calculating any values.

The type after conversion is compared with type-names from the list of associations.

If the type is compatible with the type-name of one of the associations, then the type, value, and value category of the generic selection are the type, value, and value category of the expression that appears after the colon for that type-name.

If none of the type-names are compatible with the type of the controlling-expression, and the default association is provided, then the type, value, and value category of the generic selection are the type, value, and value category of the expression after the default : label.

[edit] Notes

Xem thêm: Dự đoán tử vi mệnh Kim trong năm 2022

The controlling-expression and the expressions of the selections that are not chosen are never evaluated.

Because of the lvalue conversions, “abc” matches char* and not char[4] and (int const){0} matches int, and not const int.

All value categories, including function designators and void expressions, are allowed as expressions in a generic selection, and if selected, the generic selection itself has the same value category.

The type-generic math macros from <tgmath.h>, introduced in C99, were implemented in compiler-specific manner. Generic selections, introduced in C11, gave the programmers the ability to write similar type-dependent code.

XEM THÊM:  Eco là gì? Công dụng và cách dùng chế độ Eco trên điều hòa không khí

Xem thêm: Xem Tử Vi – BÀI 2 Hướng Dẫn Cách Xem Lá Số Tử Vi – Thầy Cao Anh, Học Cải Vận Mệnh – Hướng dẫn xem lá số tử vi –

Generic selection is similar to overloading in C++ (where one of several functions is chosen at compile time based on the types of the arguments), except that it makes the selection between arbitrary expressions.

[edit] Keywords

_Generic, default

[edit] Example

Run this code #include <stdio.h> #include <math.h> // Possible implementation of the tgmath.h macro cbrt #define cbrt(X) _Generic((X), long double: cbrtl, default: cbrt, float: cbrtf )(X) int main(void) { double x = 8.0; const float y = 3.375; printf(“cbrt(8.0) = %fn”, cbrt(x)); // selects the default cbrt printf(“cbrtf(3.375) = %fn”, cbrt(y)); // converts const float to float, // then selects cbrtf }


cbrt(8.0) = 2.000000 cbrtf(3.375) = 1.500000

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C standards.

DR Applied to Behavior as published Correct behavior DR 481 C11 it was underspecified if the controlling expression undergoes lvalue conversions it undergoes

[edit] References

  • C17 standard (ISO/IEC 9899:2018):
  • Generic selection (p: 56-57)
  • C11 standard (ISO/IEC 9899:2011):
  • Generic selection (p: 78-79)

[edit] See also

C++ documentation for Templates

Vậy là đến đây bài viết về Generic selection đã dừng lại rồi. Hy vọng bạn luôn theo dõi và đọc những bài viết hay của chúng tôi trên website TTTD.VN

Chúc các bạn luôn gặt hái nhiều thành công trong cuộc sống!

Trả lời

Email của bạn sẽ không được hiển thị công khai.