简体   繁体   English

C ++中的“内置”功能

[英]“built-in” functions in C++

I'm beginner at C++ so if the answer is obvious it possibly is the one I'm looking for. 我是C ++的初学者,所以如果答案很明显,可能是我正在寻找的答案。 I was reading the second response in this thread and got confused. 我正在阅读该线程中的第二个响应,并感到困惑。

#include <algorithm>
#include <cassert>

int
main()
{
    using std::swap;
    int a(3), b(5);
    swap(a, b);
    assert(a == 5 && b == 3);
}

What I don't get is "This is just a defined function. What I meant was, why it is not directly built-in" but there was no need to include a new library so isn't it built-in? 我没有得到的是“这只是一个已定义的函数。我的意思是,为什么它不是直接内置的”,但是没有必要include新的库,所以它不是内置的吗? Does the std library automatically get imported (if yes, why doesn't the namespace automatically get set to std)? std库是否自动导入(如果是,为什么不将命名空间自动设置为std)?

This is just a defined function. 这只是一个定义的功能。 What I meant was, why it is not directly built-in" but there was no need to include a new library so isn't it built-in? 我的意思是,为什么它不是直接内置的”,但是没有必要包含一个新库,所以它不是内置的吗?

The C++ library is a part of C++, by definition. 根据定义,C ++库是C ++的一部分。 However, it is not a part of the core language. 但是,它不是核心语言的一部分。 C++ is a huge, huge language. C ++是一门巨大的语言。 Making it so the compiler knew every nook and cranny of the language right off the bat would make the compiler huge and slow to load. 如此一来,编译器就会立即知道该语言的每一个细节,这将使编译器变得庞大且加载缓慢。 The philosophy instead is to keep the core somewhat small and give programmers the ability to extend the functionality by #including header files that. 相反,其理念是使内核保持较小的尺寸,并使程序员能够通过#including头文件的功能来扩展功能。 specify what they need. 明确说明他们需要什么。

Why doesn't the namespace automatically get set to std? 为什么命名空间没有自动设置为std?

That would essentially make all kinds of very common words into keywords. 从本质上讲,这会将各种非常常见的词变成关键字。 The list of words you shouldn't use (keywords, global function in C, words reserved by POSIX or Microsoft, ...) is already huge. 您不应该使用的单词列表(关键字,C语言中的全局函数,POSIX或Microsoft保留的单词等等)已经很大。 Putting the C++ standard library in namespace std is a feature. 将C ++标准库放在命名空间std中是一项功能。 Putting all of those names in the global namespace would be a huge misfeature. 将所有这些名称放在全局名称空间中将是一个巨大的错误。

"This is just a defined function. What I meant was, why it is not directly built-in" but there was no need to import a new library so isn't it built-in? “这只是一个定义的函数。我的意思是,为什么它不是直接内置的”,但是不需要导入新的库,所以它不是内置的吗? Does the std library automatically get imported (if yes, why doesn't the namespace automatically get set to std)? std库是否自动导入(如果是,为什么不将命名空间自动设置为std)?

Well, by defined function it means, most likely that the function is already pre-written, and defined in the library , it isn't directly built-in probably because it was designed that way; 好的,通过已defined function这意味着该函数很可能已经预先编写并在库中定义,它不是直接内置的,可能是因为它是按这种方式设计的; only core essentials were included in the language, and everything else is in a library so the programmer can import what they want . 该语言仅包含核心要素,其他所有内容都在库中,以便程序员可以导入所需内容

By built-in, usually it's a keyword, like for or while . 内置而言,通常是关键字,例如forwhile

And no, std isn't automatically imported since it's designed so the programmer can choose what namespaces they want, like custom namespaces or std. 而且不,因为std是经过设计的,所以不会自动导入,因此程序员可以选择所需的命名空间,例如自定义命名空间或std。 An example of this being bad to automatically have std is this: 无法自动拥有std的一个例子是:

Say you automatically defined std, then you wanted to do using namespace foo; 假设您自动定义了std,那么您想using namespace foo;来完成using namespace foo; , now if foo also had function cout , you would run into a huge problem, like say you wanted to do this; ,现在,如果foo也具有cout函数,您将遇到一个巨大的问题,例如说您想这样做;

// code above
cout << "Hello, World" << endl;
// code below

how would the compiler which namespace function to use use? 编译器将如何使用哪个命名空间函数? the default or your foo namespace cout ? 默认还是您的foo名称空间cout In order to prevent this, there is no default namespace set, leaving it up to the programmer. 为了防止这种情况,没有设置默认的名称空间,将其留给程序员。

Hope that helps! 希望有帮助!

In your code, you have the line: 在代码中,您需要执行以下操作:

using std::swap;

So the call to swap doesn't need std:: . 因此,调用swap不需要std:: For assert , it was defined as a macro, so it also would not need std:: . 对于assert ,它被定义为宏,因此也不需要std:: If you did not use using , then other than macros, you would have to use std:: to refer to functions and objects provided by the standard C++ library. 如果您没有使用using ,那么除了宏之外,您必须使用std::来引用标准C ++库提供的函数和对象。

The standard C++ library normally get linked into your program when you compile your program to create an executable. 在编译程序以创建可执行文件时,通常会将标准C ++库链接到程序中。 From this point of view, you might consider it "built-in". 从这个角度来看,您可能会认为它是“内置”的。 However, the term "built-in" would usually mean the compiler treats the word swap like a keyword, which is not the case here. 但是,术语“内置”通常意味着编译器将单词swap视为关键字,在此情况并非如此。 swap is a template function defined in the algorithm header file, and assert is a macro defined in cassert . swap是在algorithm头文件中定义的模板函数, assert是在cassert定义的宏。

A namespace is a convenience to allow parts of software to be easily partitioned by name. 命名空间是一种便利功能,它允许按名称轻松划分软件的各个部分。 So if you wanted to define your own swap function, you could put it into your own namespace. 因此,如果您想定义自己的swap函数,则可以将其放入自己的命名空间中。

namespace mine {
    template <typename T> void swap (T &a, T &b) { /*...*/ }
}

And it would not collide with the standard, or with some library that defined swap without a namespace. 它不会与标准冲突,也不会与定义了交换而没有名称空间的某些库冲突。

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

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