Last updated:2026-10-12
C++17 Structured Bindings¶
Before talking about structured bindings, here are a few interview questions - can you answer them?
-
How do you access the private members of a custom class using structured bindings?
-
How do you modify the members of a custom class using structured bindings?
Few people can answer these questions, and neither the questions nor the answers can be found online. Based on these questions, this article introduces its topic: how to support structured bindings for custom types, and what pitfalls lie along the way.
Note: for the lazy, the answers and code are already available in the members' community.
To make this article flow more smoothly, let's start from an example.
1.Custom types¶
Given a class:
struct Foo {
Foo(int64_t id, std::string data) : id_(id), data_(std::move(data)) {}
void Print() { std::cout << "id = " << id_ << ", data = " << data_ << std::endl; }
private:
int64_t id_;
std::string data_;
};
To make the following code run:
int main() {
Foo f{12, "hello world"};
const auto& [id, data] = f;
std::cout << "id = " << id << ", data = " << data << std::endl;
f.Print();
}
If you run it directly, it fails to compile:
So what do we do?
2.Supporting structured bindings¶
We solve this problem in two steps:
Step 1: specialize std::tuple_size and std::tuple_element
namespace std {
template <>
struct tuple_size<Foo> : std::integral_constant<std::size_t, 2> {};
template <>
struct tuple_element<0, Foo> {
using type = int64_t;
};
template <>
struct tuple_element<1, Foo> {
using type = std::string;
};
}
Step 2: add a friend get function for the struct above, then implement it:
template <std::size_t Index>
decltype(auto) get(const Foo& f);
template <>
decltype(auto) get<0>(const Foo& f) {
return f.id_;
}
template <>
decltype(auto) get<1>(const Foo& f) {
return f.data_;
}
Now it works correctly, producing the output:
Finally, here are a few questions to leave you with:
- Above we provided read-only access; what happens if the bound objects are modified when accessing via const auto&?
For example:
- How do you support modifying access to class members?
That is, make this statement work:
That's all for this section; feel free to discuss in the comments.
Related Topics¶
- C++17 [[nodiscard]] Attribute | C++那些事
- C++17 std::string_view | C++那些事
- C++17 PMR Memory Resources | C++那些事
- C++17 Seventeen Advanced Features | C++那些事
- C++17 Class Template Argument Deduction CTAD | C++那些事
- C++17 optional_ref | C++那些事
- C++17 std::variant | C++那些事
- C++17 Template auto Deduction | C++那些事
- C++17 Variable Templates | C++那些事