C++ Concepts library
Return to Concept
https://en.cppreference.com/w/cpp/keyword/concept
- “concepts – a C++20 feature that provides direct support for compile-time constraints on template parameters (limiting which potential template arguments match) to appropriately narrow the applicability of a template. Additionally, concepts can be used to add ordering between constrained templates — i.e., more constrained and less constrained templates can be implemented differently, and the most constrained one that is applicable for a particular invocation will be preferred for instantiation. Moreover, concepts afford advantages with respect to compile-time error detection and especially diagnostics. Prior to C++20, much of the same functionality was available using SFINAE and other advanced template metaprogramming techniques, but, among other things, concepts make expressing the requirements on template parameters simpler and clearer and allow constraining nontemplate constructors of class templates. static_assert (122), auto Variables (208), Generalized PODs ’11 (480), initializer_list (571), auto Return (1201)” (EMCppSfe 2021)
- Snippet from Wikipedia: Concepts (C++)
Concepts are an extension to the templates feature provided by the C++ programming language. Concepts are named Boolean predicates on template parameters, evaluated at compile time. A concept may be associated with a template (class template, function template, member function of a class template, variable template, or alias template), in which case it serves as a constraint: it limits the set of arguments that are accepted as template parameters.
Originally dating back to suggestions for C++11, the original concepts specification has been revised multiple times before formally being a required part of C++20.
https://en.cppreference.com/w/cpp/concepts
The concepts library provides definitions of fundamental library concepts that can be used to perform compile-time validation of template arguments and perform function dispatch based on properties of types. These concepts provide a foundation for equational reasoning in programs.
Most concepts in the standard library impose both syntactic and semantic requirements. It is said that a standard concept is satisfied if its syntactic requirements are met, and is modeled if it is satisfied and its semantic requirements (if any) are also met.
In general, only the syntactic requirements can be checked by the compiler. If the validity or meaning of a program depends whether a sequence of template arguments models a concept, and the concept is satisfied but not modeled, or if a semantic requirement is not met at the point of use, the program is ill-formed, no diagnostic required.
Additional concepts can be found in the iterators library, the algorithms library, and the ranges library.
© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers
SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.