鉴于这些声明:
int a[3] {10,20,30};
std::tuple<int,int,int> b {11,22,33};
我可以使用结构化绑定(bind)声明来解码 a
和 b
:
auto [x1,y1,z1] = a;
auto [x2,y2,z2] = b;
但是如果 x1
、y1
等已经存在,我该怎么办?
std::tie(x1,y1,z1) = a; // ERROR
std::tie(x2,y2,z2) = b; // OK
这适用于 b
但不适用于 a
。是否有适用于 a
的类似简单构造,或者我是否必须获取 a[0]
、a[1]
和 a[2]
分开?
最佳答案
没有。
结构化绑定(bind)有特定的语言规则来处理数组和某些其他类型。 tie()
具体是tuple<T&...>
并且只能从另一个 tuple<U&...>
分配.
对于数组的情况,您可以编写一个函数将该数组转换为引用元组:
template <typename T, size_t N, size_t... Is>
auto as_tuple_impl(T (&arr)[N], std::index_sequence<Is...>) {
return std::forward_as_tuple(arr[Is]...);
}
template <typename T, size_t N>
auto as_tuple(T (&arr)[N]) {
return as_tuple_impl(arr, std::make_index_sequence<N>{});
}
std::tie(x1, y1, z1) = as_tuple(a); // ok
或者,如果您知道有多少绑定(bind)(无论如何您都必须知道),您可以使用结构化绑定(bind)作为返回元组。但是你必须指定大小并为每个大小写一个案例:
template <size_t I, typename T>
auto as_tuple(T&& tuple) {
if constexpr (I == 1) {
auto&& [a] = std::forward<T>(tuple);
return std::forward_as_tuple(a);
} else if constexpr (I == 2) {
auto&& [a, b] = std::forward<T>(tuple);
return std::forward_as_tuple(a, b);
} else if constexpr (I == 3) {
// etc.
}
}
std::tie(x1, y1, z1) = as_tuple<3>(a); // ok
关于c++ - 结构化绑定(bind)和 tie(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49574460/