结构化绑定可以绑定结构体、数组和 tuple-like 对象。
完整示例:
#include <iostream>
#include <format>
#include <iomanip>void test_00(){struct Box{int width_;int height_;std::string name_;};Box box{3,4,"amazing"};auto [w, h, name]{box};//auto [w, h, name] = box;std::cout << "w=" << box.width_ << ", h=" << box.height_ << ", name=" << box.name_ << std::endl; // w=3, h=4, name=amazing
}void test_01(){struct Box{int width_;int height_;std::string name_;};Box box{3,4,"amazing"};//auto [w, h, name]{box};auto [w, h, name] = box;std::cout << std::format("w={}, h={}, name={}\n", w, h, name); // w=3, h=4, name=amazingstd::cout << std::quoted(box.name_) << std::endl; // "amazing"
}int main(){test_00();test_01();
}