我正在学习结构化绑定声明。我的理解是,在auto& [x, y] = expr;
变量x
中y
引入了“引用std::tuple_element<i, E>::type
”类型(对于i=0, 1
和E
是不可见变量的类型e
)。此外,这些变量用 初始化get<i>(e)
。
因此,如果我使用auto&
并get<>
返回一个值(不是引用),它不应该编译,因为您不能将左值绑定到临时值。但是,以下示例在 GCC、Clang 和 Visual Studio 的某些版本中为我构建:
#include <cstddef>
#include <tuple>
#include <type_traits>
struct Foo {
template<std::size_t i>
int get() { return 123; }
};
namespace std {
template<> struct tuple_size<Foo> : integral_constant<size_t, 1> {};
template<std::size_t i> struct tuple_element<i, Foo> { using type = int; };
}
int main() {
Foo f;
auto& [x] = f;
x++;
}
此外,C++ Insights清楚地表明,clang 将结构化绑定扩展到:
Foo f = Foo();
Foo & __f17 = f;
std::tuple_element<0, Foo>::type x = __f17.get<0>();
x++;
在这里,它声明x
的不是引用,而是一个值。这是为什么?
我预计左值引用和编译错误:(e
在__f17
上面的示例中)是左值引用。