如何在C ++中查看标准库函数? [英] How to view standard library functions in C++?

查看:97
本文介绍了如何在C ++中查看标准库函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想查看函数 toupper()的代码以了解其工作原理,有什么办法吗?我已经搜索并打开了 string.h 库,但未找到任何内容.

For example, i want to see the code of function toupper() to understand how it works, is there any way? I have searched and opened string.h library, but didn't find anything.

推荐答案

从严格的语言角度来看,您不能看到标准函数的代码",因为C ++语言标准仅定义函数的原型和行为,而不是它们的实现方式.

From a strict language point of view, you cannot "see the code" of a standard function, because the C++ language standard only defines functions' prototypes and behaviours, not how they are implemented.

实际上,从严格的语言角度来看,像 toupper 这样的标准函数甚至不必具有源代码,因为像< string.h>这样的标准标头是必需的.甚至不必是文件!

In fact, from a strict language point of view, a standard function like toupper does not even have to have source code, because a standard header, like <string.h> does not even have to be a file!

当然,在实践中,您可能永远不会遇到标准标头不是文件的C ++实现,因为文件只是标头的自然而简单的实现.这意味着实际上,对于标题< string.h> ,实际上在计算机上的某个位置存在一个名为"string.h"的C ++源文件.找到它并打开它.

Of course, in practice, you will probably never encounter a C++ implementation in which standard headers are not files, because files are just a natural and simple implementation of headers. This means that in practice, for the header <string.h>, there is actually a C++ source file called "string.h" somewhere on your computer. Just find it and open it.

我已经搜索并打开了 string.h 库,但是没有找到任何东西.

I have searched and opened string.h library, but didn't find anything.

那么您看起来还不够近.提示:此文件很可能包含一个或多个其他头文件.

Then you have not looked close enough. Hint: This file most likely includes one or more other header files.

请注意,如果您实际查找的是 toupper ,则该函数始终不在< string.h> 中.改为查看< ctype.h> .cppreference.com是一个很好的在线参考,可以告诉您哪些标头包含哪些功能.

Note that if you actually looked for toupper, that function is not in <string.h> anyway. Look in <ctype.h> instead. cppreference.com is a good online reference to tell you which headers contain which functions.

http://en.cppreference.com/w/c/string/字节/高位

同样,这并不意味着编译器的相应头文件直接包含该功能,而是可能直接或间接包括其他包含该功能的文件.

Again, this does not mean that the corresponding header file of your compiler contains that function directly, but it may directly or indirectly include some other file which contains it.

无论如何,要提防您将在编译器的头文件中看到的内容.它通常会比您想象的要复杂得多,更重要的是,它将经常使用您不允许在自己的代码中使用的构造.毕竟,这些文件中的代码是编译器实现的内部代码,并且编译器具有许多您没有的特权,例如,使用否则受禁止的标识符,例如 _STD_BEGIN .还期望有很多完全非标准的 #pragma s和其他非便携式的东西.

In any case, beware of what you will see inside of your compiler's header files. It will usually be a lot more complicated than you may think, and, more importantly, it will often use constructs you are not allowed to use in your own code; after all, the code in those files is internal to the compiler implementation, and the compiler has a lot of privileges you don't have, for example using otherwise forbidden identifiers like _STD_BEGIN. Also expect a lot of completely non-standard #pragmas and other non-portable stuff.

要记住的另一重要事项是,您不应该深入研究函数的实现来发现它的作用.在编写糟糕的软件(即界面混乱且没有文档的软件(在现实世界中到处都是))中,不幸的是,只要您可以访问源代码,就必须这样做.

Another important thing to keep in mind is that you are not supposed to dig through a function's implementation to find out what it does. In badly written software, i.e. software with confusing interfaces and no documentation (which exists everywhere in the real world), you unfortunately have to do this, provided you have access to the source code.

但是C ++标准函数有完整的文档记录,并且具有一些经过精心设计的接口(有一些可争议的例外).研究它们在您的系统上的实现可能很有趣,而且有教育意义,有时甚至是调试所必需的,但不要让这种可能性使您无法学习两种重要的软件工程技能:

But C++ standard functions are perfectly documented and have, with some arguable exceptions, well-designed interfaces. It may be interesting, and educating, and sometimes even necessary for debugging, to look into their implementation on your system, but don't let this possibility keep you from learning two important software-engineering skills:

  1. 阅读文档.
  2. 编程为接口,而不是实现.

这篇关于如何在C ++中查看标准库函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆