C++ attribute: carries_dependency (since C++11) (removed in C++26)
| General topics | ||||||||||||||||
| Flow control | ||||||||||||||||
| Conditional execution statements | ||||||||||||||||
| Iteration statements (loops) | ||||||||||||||||
|
||||||||||||||||
| Jump statements | ||||||||||||||||
| Functions | ||||||||||||||||
| Function declaration | ||||||||||||||||
| Lambda function expression | ||||||||||||||||
inline
specifier
|
||||||||||||||||
| Dynamic exception specifications ( until C++17* ) | ||||||||||||||||
noexcept
specifier
(C++11)
|
||||||||||||||||
| Exceptions | ||||||||||||||||
| Namespaces | ||||||||||||||||
| Types | ||||||||||||||||
| Specifiers | ||||||||||||||||
|
||||||||||||||||
| Storage duration specifiers | ||||||||||||||||
| Initialization | ||||||||||||||||
| Expressions | ||||||||||||||||
| Alternative representations | ||||||||||||||||
| Literals | ||||||||||||||||
| Boolean - Integer - Floating-point | ||||||||||||||||
| Character - String - nullptr (C++11) | ||||||||||||||||
| User-defined (C++11) | ||||||||||||||||
| Utilities | ||||||||||||||||
| Attributes (C++11) | ||||||||||||||||
| Types | ||||||||||||||||
typedef
declaration
|
||||||||||||||||
| Type alias declaration (C++11) | ||||||||||||||||
| Casts | ||||||||||||||||
| Memory allocation | ||||||||||||||||
| Classes | ||||||||||||||||
| Class-specific function properties | ||||||||||||||||
|
||||||||||||||||
| Special member functions | ||||||||||||||||
|
||||||||||||||||
| Templates | ||||||||||||||||
| Miscellaneous | ||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++23)
|
||||
|
carries_dependency
(C++11)
(until C++26)
|
||||
|
(C++14)
|
||||
|
(C++17)
|
||||
|
(C++26)
|
||||
|
(C++20)
|
||||
|
(C++17)
|
||||
|
(C++17)
|
||||
|
(C++11)
|
||||
|
(C++20)
|
||||
|
(TM TS)
|
||||
|
(C++20)
|
Indica que la cadena de dependencia en liberación-consumo std::memory_order se propaga hacia y desde la función, lo que permite al compilador omitir instrucciones innecesarias de barrera de memoria.
Contenidos |
Sintaxis
[
[
carries_dependency
]
]
|
|||||||||
Explicación
Este atributo puede aparecer en dos situaciones:
Este atributo debe aparecer en la primera declaración de una función o uno de sus parámetros en cualquier unidad de traducción. Si no se utiliza en la primera declaración de una función o uno de sus parámetros en otra unidad de traducción, el programa está mal formado; no se requiere diagnóstico.
Ejemplo
Adaptado casi sin cambios de SO .
#include <atomic> #include <iostream> void print(int* val) { std::cout << *val << std::endl; } void print2(int* val [[carries_dependency]]) { std::cout << *val << std::endl; } int main() { int x{42}; std::atomic<int*> p = &x; int* local = p.load(std::memory_order_consume); if (local) { // The dependency is explicit, so the compiler knows that local is // dereferenced, and that it must ensure that the dependency chain // is preserved in order to avoid a fence (on some architectures). std::cout << *local << std::endl; } if (local) { // The definition of print is opaque (assuming it is not inlined), // so the compiler must issue a fence in order to ensure that // reading *p in print returns the correct value. print(local); } if (local) { // The compiler can assume that although print2 is also opaque then // the dependency from the parameter to the dereferenced value is // preserved in the instruction stream, and no fence is necessary (on // some architectures). Obviously, the definition of print2 must actually // preserve this dependency, so the attribute will also impact the // generated code for print2. print2(local); } }
Salida posible:
42 42 42
Referencias
- Estándar C++23 (ISO/IEC 14882:2024):
-
- 9.12.4 Atributo carries dependency [dcl.attr.depend]
- Estándar C++20 (ISO/IEC 14882:2020):
-
- 9.12.3 Atributo transmite dependencia [dcl.attr.depend]
- Estándar C++17 (ISO/IEC 14882:2017):
-
- 10.6.3 Atributo de dependencia de portado [dcl.attr.depend]
- Estándar C++14 (ISO/IEC 14882:2014):
-
- 7.6.4 Atributo transporta dependencia [dcl.attr.depend]
- Estándar C++11 (ISO/IEC 14882:2011):
-
- 7.6.4 Atributo de dependencia de transporte [dcl.attr.depend]
Véase también
|
(C++11)
(deprecated in C++26)
|
elimina el objeto especificado del
std::memory_order_consume
árbol de dependencias
(plantilla de función) |