C++ 中是否存在 typename 关键字,以便向后兼容“C 模板”?

发布于 2024-08-22 13:35:30 字数 272 浏览 4 评论 0原文

我正在上 C++ 课程,我的老师顺便提到 C++ 中存在 typename 关键字(而不是在模板声明中使用 class 关键字),例如向后兼容“C 模板”。

这让我大吃一惊。我从未在 ANSI C 中看到或听说过类似 C++ 模板的东西(也许除了预处理器……而且这根本不是同一件事)。那么,我是否错过了一些东西 >巨大的某处,或者这是gcc或其他东西的真正深奥的扩展,或者我的老师是不是离谱了?

I’m taking a C++ class, and my teacher mentioned in passing that the typename keyword existed in C++ (as opposed to using the class keyword in a template declaration), for backwards compatibility with “C templates.”

This blew my mind. I’ve never seen or heard tell of anything like C++’s templates (except, perhaps, the preprocessor… and that’s not really the same thing at all) in ANSI C. So, did I miss something huge somewhere, or is this a really esoteric extension by gcc or something, or is my teacher way off-base?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

对你的占有欲 2024-08-29 13:35:31

我想说的是,C 确实没有原生模板,但是,您可以使其与某种 元编程看看 web 你会发现如何...

另一件重要的事情是,C 是一种通用的编程语言,所以很多事情,比如面向对象、模板和其他一些事情都可以通过更多的努力来完成。

像 Gnome 这样的项目证明了它可以完成并且做得很好。

PS:抱歉我的英语很糟糕!

I wish to say that C really don't have a native template stuff, however, you can make it works fine with some kind of a MetaProgramming, take a look around the web you will find how to...

Another important thing to say is that C is a programming language to general purpose, so a lot of things like Object Orientation, template and some other things can be done with a little more effort.

Projects like Gnome are proof that it can be done and very well.

P.S.: Sorry about my terrible english!!!

满栀 2024-08-29 13:35:30

我认为你的老师有失偏颇。

请参阅 Stan Lippman 的帖子:为什么 C++ 支持类型参数的类和类型名 这就是 C++ 支持两者的真正原因。

I think your teacher is off base.

See Stan Lippman's post: Why C++ Supports both Class and Typename for Type Parameters for the real reason why C++ supports both.

江南烟雨〆相思醉 2024-08-29 13:35:30

也许您老师的目标短语是“...为了与 C 类型向后兼容”,即认识到 template< 的问题。当 T 是 C 风格的内置类型(例如 charint)时,class T> 会产生误导,就像其他人那样说。 但这不是一个类!:-)

不久前,一些 GCC 人员建议 让模板机制可供 C 编译器使用将是实现 C99 的 的好方法,但这并没有出现已经达到任何目标。

Perhaps the phrase your teacher was aiming for was along the lines of "...for backwards compatibility with C types", i.e., recognizing the problem that template<class T> is misleading when T is a C-style built-in type such as char or int, as others have said. But that's not a class! :-)

A while back a few GCC folks were suggesting that making the template machinery available to the C compiler would be a good way to implement C99's <tgmath.h>, but that doesn't appear to have come to anything.

神仙妹妹 2024-08-29 13:35:30

你的老师正在编造事情。 C 中不存在模板之类的东西。 typename 关键字的存在有两个原因:

  1. template 比说 template< 更有意义;class T> 因为 T 可以是非类类型,如 intdouble

  2. 它可用于解决 A::B * foo; 等声明中的解析歧义。这是声明一个名为 foo 的变量,还是一个乘法表达式? (答案:它被解析为后者。要使其成为声明,请编写 typename A::B *foo; ,它告诉编译器将 A::B 解释为类型名称,而不是变量名称。)

请参阅 http://pages.cs.wisc。 edu/~driscoll/typename.html 了解详细说明。

Your teacher is making things up. There's no such thing as templates in C. The typename keyword exists for two reasons:

  1. It makes more sense to say template<typename T> than template<class T> since T can be non-class types like int or double.

  2. It can be used to resolve parsing ambiguities in declarations like A::B * foo;. Does this declare a variable named foo, or is it a multiplication expression? (Answer: It's parsed as the latter. To make it a declaration write typename A::B *foo; which tells the compiler to interpret A::B as a type name, not a variable name.)

See http://pages.cs.wisc.edu/~driscoll/typename.html for a detailed explanation.

帅气尐潴 2024-08-29 13:35:30

不,不存在 C 模板之类的东西。 typename 甚至不是 C 中的关键字。

No, there's no such thing as a C template. typename isn't even a keyword in C.

站稳脚跟 2024-08-29 13:35:30

这似乎不对。 typename 在 C 中根本不是保留字。

也许他们说错/记错了,并想到了“C with Classes”。

This doesn't seem right. typename is not a reserved word at all in C.

Perhaps they misspoke/remembered and were thinking of "C with Classes".

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