简体   繁体   English

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

[英]Does the typename keyword exist in C++, for backwards compatibility with “C templates?”

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.” 我正在学习C ++课程,而我的老师在传递中提到C ++中存在typename关键字(而不是在模板声明中使用class关键字),以便向后兼容“C模板”。

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? 我从来没有见过或听说过C ++的模板之类的东西(除了,也许是预处理器......在ANSI C中根本不是同一个东西)。所以,我是否错过了某个地方的巨大东西,或者这是一个非常深奥的东西通过gcc或其他东西扩展,还是我的老师离开基地?

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. 请参阅Stan Lippman的帖子:为什么C ++支持类型参数的类和类型名称 ,这是C ++支持这两者的真正原因。

Perhaps the phrase your teacher was aiming for was along the lines of "...for backwards compatibility with C types ", ie, 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. 也许你的老师所针对的短语是“......为了与C 类型向后兼容”,即,当T是C风格的内置类型时,识别template<class T>会产生误导的问题。正如其他人所说的那样,作为charint 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. 不久之前,一些GCC人员建议将模板机制提供给C编译器将是实现C99的<tgmath.h>的好方法,但这似乎没有任何结果。

Your teacher is making things up. 你的老师正在努力。 There's no such thing as templates in C. The typename keyword exists for two reasons: C中没有模板这样的东西typename关键字的存在有两个原因:

  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 . template<typename T>template<class T>更有意义,因为T可以是非类型类型,如intdouble

  2. It can be used to resolve parsing ambiguities in declarations like A::B * foo; 它可用于解析A::B * foo;等声明中的解析歧义A::B * foo; . Does this declare a variable named foo , or is it a multiplication expression? 这是声明一个名为foo的变量,还是一个乘法表达式? (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.) (答案:它被解析为后者。要使它成为声明,写入类型名称typename A::B *foo;它告诉编译器将A::B解释为类型名称,而不是变量名称。)

See http://pages.cs.wisc.edu/~driscoll/typename.html for a detailed explanation. 有关详细说明,请参见http://pages.cs.wisc.edu/~driscoll/typename.html

No, there's no such thing as a C template. 不,没有C模板这样的东西。 typename isn't even a keyword in C. typename甚至不是C中的关键字。

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... 我想说C真的没有原生模板的东西,但是,你可以通过某种MetaProgramming使其工作正常, 看看你会发现如何...

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. 另一个重要的事情是C是一种通用的编程语言,因此可以通过更多努力完成诸如面向对象,模板和其他一些事情。

Projects like Gnome are proof that it can be done and very well. 像Gnome这样的项目证明它可以做得非常好。

PS: Sorry about my terrible english!!! PS:对不起我糟糕的英语!

This doesn't seem right. 这似乎不对。 typename is not a reserved word at all in C. 在C中,typename根本不是保留字。

Perhaps they misspoke/remembered and were thinking of "C with Classes". 也许他们错过了/记住了,并且正在考虑“C with Classes”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM