本文介绍了有关最多的Vexing解析的混乱细节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何将以下行解析为一个函数声明:

My question is how the following line can be parsed as a function declaration:

vector<int> v(istream_iterator<int>(cin), istream_iterator<int>());



我理解了Most Vexing Parse的大部分细节,以及为什么第二个临时迭代器可以解释为一个类型是一个函数返回一个迭代器,没有参数,但我没有得到的是为什么第一个临时迭代器可以解释为一个类型。它代表什么类型?我的想法是,它将是一些类型的函数类型,但我不能看到如何使用名称 cin 。是否声明该参数是 istream_iterator< int> 命名为 cin ?如果是这样,这是否意味着你可以任意地括号函数的参数的名称?如果是,为什么?

I understand most of the details of the Most Vexing Parse and why the second temporary iterator can be interpreted as a type that is a function returning an iterator and taking no arguments, but what I don't get is why the first temporary iterator can be interpreted as a type. What type does it represent? My thought is that it would be some sort of function type, but I can't see how the name cin gets used. Is it declaring that the parameter is an istream_iterator<int> named cin? If so, does that mean that you can arbitrarily parenthesize the names of arguments to functions? And if so, why?

推荐答案

istream_iterator< int>(cin) istream_iterator< int> cin 但是具有多余的括号。这个声明符语法继承自C,我认为即使C的发明者(Ken Thompson?)把它描述为一个错误。

istream_iterator<int>(cin) is exactly the same as istream_iterator<int> cin but with superfluous parens. This declarator syntax was inherited from C, and I think even the inventor of C (Ken Thompson?) described it as a mistake.

这篇关于有关最多的Vexing解析的混乱细节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 03:31