简体   繁体   English

C++ #include<XXX.h> 相当于 Python 的 import XXX as X

[英]C++ #include<XXX.h> equivalent of Python's import XXX as X

I work with Python most of the time, for some reasons now I also need to use C++.我大部分时间使用 Python,出于某些原因,我现在还需要使用 C++。

I find Python's import XXX as X very neat in the following way, for example:我发现 Python 的 import XXX as X 通过以下方式非常简洁,例如:

import numpy as np
a = np.array([1,2,3])

where I'm very clear by looking at my code that the array() function is provided by the numpy module.通过查看我的代码,我非常清楚array()函数是由numpy模块提供的。

However, when working with C++, if I do:但是,在使用 C++ 时,如果我这样做:

#include<cstdio>
std::remove(filename);

It's not clear to me at first sight that remove() function under the std namespace is provided by <cstdio> .乍一看,我不清楚std命名空间下的remove()函数是由<cstdio>提供的。

So I'm wondering if there is a way to do it in C++ as the import XXX as X way in Python?所以我想知道是否有办法在 C++ 中像 Python 中的import XXX as X方式那样做?

Nope.不。

It'll be slightly clearer if you write std::remove (which you should be doing anyway; there's no guarantee that the symbol is available in the global namespace) because then at least you'll know it comes from a standard header.如果您编写std::remove (无论如何您都应该这样做;不能保证该符号在全局命名空间中可用),它会稍微清晰一些,因为至少您会知道它来自标准标头。

Beyond that, it's up to your memory.除此之外,这取决于你的记忆。 😊 😊

Some people try to introduce hacks like:有些人试图引入黑客,如:

namespace SomeThing {
   #include <cstdio>
}

// Now it's SomeThing::std::remove

That might work for your own headers (though I'd still discourage it even then).这可能适用于您自己的标题(尽管我仍然不鼓励这样做)。 But it'll cause all manner of chaos with standard headers for sure and is not permitted:但这肯定会导致标准标题的各种混乱,并且是不允许的:

[using.headers]/1: The entities in the C++ standard library are defined in headers, whose contents are made available to a translation unit when it contains the appropriate #include preprocessing directive . [using.headers]/1: C++ 标准库中的实体在头文件中定义,当翻译单元包含适当的#include预处理指令时,其内容可供翻译单元使用。

[using.headers]/3: A translation unit shall include a header only outside of any declaration or definition, and shall include the header lexically before the first reference in that translation unit to any of the entities declared in that header. [using.headers]/3:翻译单元应仅在任何声明或定义之外包含标题,并且应在该翻译单元中对在该标题中声明的任何实体的第一次引用之前在词法上包括标题。 No diagnostic is required.不需要诊断。

Recall that #include and import are fundamentally different things.回想一下, #includeimport是根本不同的东西。 C++ modules may go some way towards this sort of functionality, perhaps, but by including source code you are not even touching namespaces of symbols created by that code. C++ 模块可能会在某种程度上实现这种功能,也许,但是通过包含源代码,您甚至不会触及由该代码创建的符号的名称空间。

No there is no way to force this syntax .不,没有办法强制使用这种语法 The person who developped the code that you include is free.开发您包含的代码的人是免费的。 Generally people split their code into namespaces, which can result to this syntax:通常人们将他们的代码拆分为命名空间,这可能会导致以下语法:

#include <MyLibrary.h>

int main()
{
    MyLibrary::SayHello();
    return 0;
}

But you have no guarentee on how the code in the header is written.但是您无法保证标头中的代码是如何编写的。

C++ #include<XXX.h> equivalent of Python's import XXX as X C++ #include<XXX.h>相当于 Python 的 import XXX as X

There is no equivalent in C++. C++ 中没有等价物。

When you include a file into another, you get every single declaration from the included file, and you have no option of changing their names.当您将一个文件包含到另一个文件中时,您将获得包含文件中的每一个声明,并且您无法更改它们的名称。

You can add aliases for types and namespaces though, and references to objects, as well as write wrapper functions to do some of what the as X part does in Python.不过,您可以为类型和命名空间添加别名,以及对对象的引用,以及编写包装函数来执行 Python 中as X部分所做的一些事情。

It's not clear to me at first sight that remove() is provided by <cstdio> .乍一看,我不清楚 remove() 是由<cstdio>提供的。

The std namespace at least tells you that it is provided by the standard library. std命名空间至少告诉您它是由标准库提供的。

What I like to do, is document which header provides the used declarations:我喜欢做的是记录哪个标题提供了使用的声明:

#include<cstdio>  // std::remove
std::remove(filename);

That said, most IDE's can show you where an identifier is declared by ctrl-clicking or hovering over it (although this doesn't always work well when there are overloads in different headers).也就是说,大多数 IDE 可以通过按住 ctrl 单击或将鼠标悬停在标识符上来向您显示声明标识符的位置(尽管当不同标题中存在重载时,这并不总是有效)。 My primary use for inclusion comments is checking which includes can be removed after refactoring.我对包含注释的主要用途是检查哪些包含可以在重构后删除。

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

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