C++ 中是否存在 typename 关键字,以便向后兼容“C 模板”?
我正在上 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想说的是,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!!!
我认为你的老师有失偏颇。
请参阅 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.
也许您老师的目标短语是“...为了与 C 类型向后兼容”,即认识到
template< 的问题。当
会产生误导,就像其他人那样说。 但这不是一个类!:-)T
是 C 风格的内置类型(例如char
或int
)时,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 whenT
is a C-style built-in type such aschar
orint
, 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.不,ANSI C 不支持模板。
http: //publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/keyword_typename.htm
No, ANSI C does not support templates.
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/keyword_typename.htm
你的老师正在编造事情。 C 中不存在模板之类的东西。
typename
关键字的存在有两个原因:说
template
比说template< 更有意义;class T>
因为T
可以是非类类型,如int
或double
。它可用于解决
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:It makes more sense to say
template<typename T>
thantemplate<class T>
sinceT
can be non-class types likeint
ordouble
.It can be used to resolve parsing ambiguities in declarations like
A::B * foo;
. Does this declare a variable namedfoo
, or is it a multiplication expression? (Answer: It's parsed as the latter. To make it a declaration writetypename A::B *foo;
which tells the compiler to interpretA::B
as a type name, not a variable name.)See http://pages.cs.wisc.edu/~driscoll/typename.html for a detailed explanation.
不,不存在 C 模板之类的东西。
typename
甚至不是 C 中的关键字。No, there's no such thing as a C template.
typename
isn't even a keyword in C.这似乎不对。 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".