C++疯狂的 typedef :标准允许这种语法有什么意义?
老熟悉的一个:
typedef int cute_int; //cute : common and familiar syntax.
这个语法是完美的。没问题。
现在,当我们可以像上面那样编写 typedef 时,那么允许这种语法有什么意义:
int typedef crazy_int; //crazy : uncommon and unfamiliar syntax.
只是为了迷惑程序员?任何地方都需要这种语法吗(实际上我们已经有了前一种语法)?从编译器的角度来看,您怎么看?他们觉得可爱还是疯狂?或者这对编译器来说根本不重要?
顺便说一句,这段代码来自这里: 将 typename 关键字与 typedef 和 new 一起使用
如果您想知道这是否是语法错误,请在 ideone 查看此处的工作代码 。
The old familiar one:
typedef int cute_int; //cute : common and familiar syntax.
This syntax is perfect. No problem.
Now, when we can write typedefs like above, then what is the point of allowing this syntax:
int typedef crazy_int; //crazy : uncommon and unfamiliar syntax.
Just to confuse programmers? Is this syntax even needed anywhere (when in fact we've the previous one)? What do you think from the compilers' point of view? Do they find it cute or crazy? Or it doesn't matter to the compilers at all?
By the way, this code came from here :
Use of typename keyword with typedef and new
In case if you're wondering if that is syntax error, then check out the working code here at ideone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
"另一方面,您必须处理旧错误和兼容性问题例如,我认为 C 声明符语法是一个失败的实验,但我当时考虑的替代方案和改进并没有改善问题。的目的是随着 C 的发展保持语言定义的接近性。” - Bjarne Stroustrup
"The flip side of this is that you have to deal with old mistakes and with compatibility problems. For example, I consider the C declarator syntax an experiment that failed. Nevertheless, I adopted it for C++. The alternatives and improvements I considered at the time would not have improved matters. I rate is as a minor problem. The more serious problem is to maintain closeness of language definitions as C evolves." - Bjarne Stroustrup
问题是“为什么它让你感到困惑?”
该语法来自 C++ 中的声明说明符语法,非常通用,并且用于 C++ 中的许多事物。声明说明符的顺序并不重要。看看这些:
这些相当于:
如果你看它们足够长的时间,你会发现语法实际上是统一的和逻辑的。阅读 C++ 语法也可能有所帮助。
The question is "why it confuses you?"
The syntax comes from the grammar of declaration specifiers in C++, which is very general, and used for many things in C++. The order of declaration specifiers doesn't matter. Look at these:
These are equivalent to:
If you look at them long enough you can find that the syntax is actually uniform and logical. Also reading the grammar of C++ may help.
我不知道这种语法,尽管我的 g++ 似乎接受它......但从编译器的角度来看,它使解析事情变得更加困难:当你遇到
int
标记时,你不知道是否你正在解析一个类型定义(并且typedef
即将发生),或者你正在解析一个变量/函数定义......拥有
A typedef B< 的唯一意义/code> 是指您将
typedef
视为二元运算符(在类型赋值的意义上为A = B
)。I was not aware of this syntax, though my g++ seems to accept it... but from a compiler standpoint, it makes parsing things harder: when you encounter an
int
token, you don't know if you're parsing a type definition (and atypedef
is about to happen) or you're parsing a variable/function definition...The only sense it makes to have
A typedef B
is if you considertypedef
to be a binary operator (in the sense of a type assignment asA = B
).我不确定标准是否允许这样做。但正如我们所知,C++ 中有很多允许但语法不合逻辑的东西。比如这种东西
1[a], "hello"[2];// ...
I'm not sure that this is allowed by standart. But as we know there is a lot of things in c++ that allowed but have ilogical syntax. For instance this kind of things
1[a], "hello"[2];// ...