C++ attribute: carries_dependency (since C++11)(removed in C++26)
From cppreference.com
 
 
 < cpp | language | attributes 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
C++ language 
 
  
  
 General topics
 Conditional execution statements
 Iteration statements (loops)
 Jump statements
 Dynamic exception specifications (until C++17*)
noexcept specifier (C++11) Exceptions
 Namespaces
 Types
 Specifiers
 User-defined (C++11)
 Utilities
 Attributes (C++11)
 Types
 Type alias declaration (C++11)
 Casts
 Memory allocation
 Class-specific function properties
 Special member functions
 Miscellaneous
Declarations 
 
 
 
 
 
 
 
 
 
 Overview
 Specifiers
(C++11)
(C++20)
(C++20)
 Translation-unit-local (C++20)
(C++11)
(C++11)
(C++11)
 Pack indexing specifier (C++26)    
 Attributes (C++11)
 Declarators
 Block declarations
   →Structured binding declaration (C++17)
 Alias declaration (C++11)
 static_assert declaration (C++11)
 Opaque enum declaration (C++11)
 Other declarations
 Explicit template instantiation (C++11)
 Attribute declaration (C++11)
Attributes 
 
 
 
 
 
 
 
 
 
 
 
 
(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)
Indicates that dependency chain in release-consume std::memory_order propagates in and out of the function, which allows the compiler to skip unnecessary memory fence instructions.
Contents
[edit] Syntax
[[carries_dependency]]
 
 
[edit] Explanation
This attribute may appear in two situations:
1) it may apply to the parameter declarations of a function or lambda-expressions, in which case it indicates that initialization of the parameter carries dependency into lvalue-to-rvalue conversion of that object.
2) It may apply to the function declaration as a whole, in which case it indicates that the return value carries dependency to the evaluation of the function call expression.
This attribute must appear on the first declaration of a function or one of its parameters in any translation unit. If it is not used on the first declaration of a function or one of its parameters in another translation unit, the program is ill-formed; no diagnostic required.
[edit] Example
Adapted almost without change from SO.
Run this code
#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); } }
Possible output:
42 42 42
[edit] References
- C++23 standard (ISO/IEC 14882:2024):
- 9.12.4 Carries dependency attribute [dcl.attr.depend]
 
- C++20 standard (ISO/IEC 14882:2020):
- 9.12.3 Carries dependency attribute [dcl.attr.depend]
 
- C++17 standard (ISO/IEC 14882:2017):
- 10.6.3 Carries dependency attribute [dcl.attr.depend]
 
- C++14 standard (ISO/IEC 14882:2014):
- 7.6.4 Carries dependency attribute [dcl.attr.depend]
 
- C++11 standard (ISO/IEC 14882:2011):
- 7.6.4 Carries dependency attribute [dcl.attr.depend]
 
[edit] See also
(C++11)(deprecated in C++26)
(function template) [edit]