Search⌘ K

Modifiers & Limitations

Explore how to use modifiers such as const and references with structured bindings in C++17. Understand key limitations like the inability to declare structured bindings as constexpr or static, and restrictions in lambda captures. This lesson helps you grasp practical aspects and constraints of structured bindings to write clearer and more effective C++17 code.

Several modifiers can be ​used with structured bindings.

const modifiers:

C++
const auto [a, b, c, ...] = expression;

References:

C++
auto& [a, b, c, ...] = expression;
auto&& [a, b, c, ...] = expression;

For example:

C++ 17
#include <iostream>
using namespace std;
int main() {
std::pair a(0, 1.0f);
auto& [x, y] = a;
x = 10; // write access
std::cout << a.first;// a.first is now 10
}

In the example, x binds to the element in the generated object, that is a reference to a.

Now it’s also quite easy to get a reference to a tuple member: ...