问题描述
昨天我见过.
我们可以总结如下.考虑下面的示例代码:
Yesterday I've seen an interesting question here on SO about structured binding.
We can sum up it as it follows. Consider the example code below:
#include <tuple>
#include <type_traits>
int main() {
auto tup = std::make_tuple(1, 2);
auto & [ a, b ] = tup;
// the following line won't compile for a isn't a reference
// static_assert(std::is_reference_v<decltype(a)>);
}
在这种情况下,decltype(a)
是int
(可能是由于此项目符号(工作草案):
In this case decltype(a)
is int
(probably) because of this bullet (working draft):
此处是@Curious在感兴趣的评论中提供的wandbox的摘要.它表明实际上a
不是参考,仅此而已.
到目前为止,对于最初的问题如此好,OP询问为什么它是int
而不是int &
,并且标准说看起来是可以接受的答案.
Here is a snippet on wandbox provided by @Curious in the comments for those that are interested. It shows that actually a
isn't a reference, nothing more.
So far so good for the original question, OP asked why it was int
instead of int &
and the standard says that looked like an acceptable answer.
无论如何,我想知道委员会为何如此决定.归根结底,a
是指元组中的元素,我可以通过a
修改该元素.换句话说,a
的声明看起来像是引用中的一个,它的行为类似于引用,但不是引用.
Anyway, I'd like to know why the committee decided so. At the end of the day, a
refers to an element in the tuple and I can modify that element through a
. In other terms, the declaration of a
looks like the one of a reference, it behaves similarly to a reference but it's not a reference.
我可以接受这一点,但是我想知道背后的原因是什么.为什么decltype(a)
不能简单地是int &
?亵渎者可以理解的有意义的原因吗?
I can live with this, but I'd like to know what are the reasons behind that. Why decltype(a)
cannot be simply int &
? Is there a meaningful reason that a profane can understand?
推荐答案
这篇关于结构化绑定:当某物看起来像参考并且行为类似于参考,但不是参考时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!