Namespaces
Variants
Actions

Structured binding declaration (since C++17)

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
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
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

Binds the specified names to subobjects or elements of the initializer.

Like a reference, a structured binding is an alias to an existing object. Unlike a reference, a structured binding does not have to be of a reference type.

attr (optional) decl-specifier-seq ref-qualifier (optional) [ sb-identifier-list ] initializer ;
attr - sequence of any number of attributes
decl-specifier-seq - sequence of the following specifiers (following the rules of simple declaration):
(since C++26)
ref-qualifier - either & or &&
sb-identifier-list - list of comma-separated identifiers introduced by this declaration, each identifier may be followed by an attribute specifier sequence(since C++26)
initializer - an initializer (see below)


initializer may be one of the following:

= expression (1)
{ expression } (2)
( expression ) (3)
expression - any expression (except unparenthesized comma expressions)


A structured binding declaration introduces all identifiers in the sb-identifier-list as names in the surrounding scope and binds them to subobjects or elements of the object denoted by expression. The bindings so introduced are called structured bindings.

One of the identifiers in the sb-identifier-list can be preceded by an ellipsis. Such an identifier introduces a structured binding pack.

The identifier must declare a templated entity.

(since C++26)

A structured binding is an identifier in the sb-identifier-list  that is not preceded by an ellipsis, or an element of a structured binding pack introduced in the same identifier list(since C++26).

Contents