C++疯狂的 typedef :标准允许这种语法有什么意义?

发布于 2024-10-07 09:32:06 字数 649 浏览 7 评论 0原文

老熟悉的一个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沙与沫 2024-10-14 09:32:06

问题是“为什么它让你感到困惑?”

该语法来自 C++ 中的声明说明符语法,非常通用,并且用于 C++ 中的许多事物。声明说明符的顺序并不重要。看看这些:

int virtual f() const, *g(int);
int ptr1, *ptr2;
int typedef const *name1, name2;

这些相当于:

virtual int f() const;
virtual int* g(int);
int ptr1;
int *ptr2;
typedef const int* name1;
typedef const int name2;

如果你看它们足够长的时间,你会发现语法实际上是统一的和逻辑的。阅读 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:

int virtual f() const, *g(int);
int ptr1, *ptr2;
int typedef const *name1, name2;

These are equivalent to:

virtual int f() const;
virtual int* g(int);
int ptr1;
int *ptr2;
typedef const int* name1;
typedef const int name2;

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.

轮廓§ 2024-10-14 09:32:06

我不知道这种语法,尽管我的 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 a typedef 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 consider typedef to be a binary operator (in the sense of a type assignment as A = B).

萌吟 2024-10-14 09:32:06

我不确定标准是否允许这样做。但正如我们所知,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];// ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文