C++17 中结构化绑定引入的标识符有哪些类型?
What are the types of identifiers introduced by structured bindings in C++17?
据我所知,C++17 中结构化绑定引入的标识符实际上是对某些 "hidden" 变量的引用。这样
auto [ a, b ] = std::make_tuple(1, 2);
是种类等同于
auto e = std::make_tuple(1, 2);
auto& a = std::get<0>(e);
auto& b = std::get<1>(e);
但是,如果我打印出 std::is_reference<decltype(a)>::value
,我在第一种情况下得到 0
,在第二种情况下得到 1
。这是为什么?
if I print out std::is_reference<decltype(a)>::value
, I get 0 in the first case 1 in the second.
为什么即使我们可以证明 a
和 b
引用元组中的元素并且可以通过它们修改这些值?
我不是语言律师,但可能是由于标准(工作草案)的this bullet:
if e
is an unparenthesized id-expression naming a structured binding [...], decltype(e)
is the referenced type as given in the specification of the structured binding declaration
旁注。您应该使用这种形式,以便 a
和 b
引用元组中的元素:
auto tup = std::make_tuple(1, 2);
auto & [ a, b ] = tup;
它遵循一个最小的工作示例:
#include <tuple>
#include <type_traits>
#include <iostream>
int main() {
auto tup = std::make_tuple(1, 2);
auto & [ a, b ] = tup;
a = 0;
std::cout << a << ", " << std::get<0>(tup) << std::endl;
}
使用以下表达式在 Coliru. On the other side, what you get is a copy of the values 上查看:
auto [ a, b ] = std::make_tuple(1, 2);
Here是一篇解释得更好的文章,比humans.
的标准更易懂一点
To my knowledge, identifiers introduced by structured bindings in C++17 are in fact references to some "hidden" variable.
如果 "reference" 你的意思是语言结构参考,这不太正确。声明中的说明符属于您所说的 "hidden variable" 。引用限定符是可选的。您提供的代码更像这样:
const auto& e = std::make_tuple(1, 2);
using E = remove_reference_t<decltype((e))>;
std::tuple_element<0, E>::type& a = get<0>(e);
std::tuple_element<1, E>::type& b = get<1>(e);
据我所知,C++17 中结构化绑定引入的标识符实际上是对某些 "hidden" 变量的引用。这样
auto [ a, b ] = std::make_tuple(1, 2);
是种类等同于
auto e = std::make_tuple(1, 2);
auto& a = std::get<0>(e);
auto& b = std::get<1>(e);
但是,如果我打印出 std::is_reference<decltype(a)>::value
,我在第一种情况下得到 0
,在第二种情况下得到 1
。这是为什么?
if I print out
std::is_reference<decltype(a)>::value
, I get 0 in the first case 1 in the second.
为什么即使我们可以证明 a
和 b
引用元组中的元素并且可以通过它们修改这些值?
我不是语言律师,但可能是由于标准(工作草案)的this bullet:
if
e
is an unparenthesized id-expression naming a structured binding [...],decltype(e)
is the referenced type as given in the specification of the structured binding declaration
旁注。您应该使用这种形式,以便 a
和 b
引用元组中的元素:
auto tup = std::make_tuple(1, 2);
auto & [ a, b ] = tup;
它遵循一个最小的工作示例:
#include <tuple>
#include <type_traits>
#include <iostream>
int main() {
auto tup = std::make_tuple(1, 2);
auto & [ a, b ] = tup;
a = 0;
std::cout << a << ", " << std::get<0>(tup) << std::endl;
}
使用以下表达式在 Coliru. On the other side, what you get is a copy of the values 上查看:
auto [ a, b ] = std::make_tuple(1, 2);
Here是一篇解释得更好的文章,比humans.
的标准更易懂一点To my knowledge, identifiers introduced by structured bindings in C++17 are in fact references to some "hidden" variable.
如果 "reference" 你的意思是语言结构参考,这不太正确。声明中的说明符属于您所说的 "hidden variable" 。引用限定符是可选的。您提供的代码更像这样:
const auto& e = std::make_tuple(1, 2);
using E = remove_reference_t<decltype((e))>;
std::tuple_element<0, E>::type& a = get<0>(e);
std::tuple_element<1, E>::type& b = get<1>(e);