Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用指令C++;实施_C++_C_Using Directives - Fatal编程技术网

C++ 使用指令C++;实施

C++ 使用指令C++;实施,c++,c,using-directives,C++,C,Using Directives,在C中,如果我使用#include“someFile.h”,预处理器将执行文本导入,这意味着someFile.h的内容将“复制并粘贴”到#include行上。在C++中,有using指令。这是否与#include的工作方式类似,即:名称空间的文本导入 using namespace std; // are the contents of std physically inserted on this line? 如果不是这样,那么使用指令是如何实现的。不,它没有。这意味着从这一行开始,您可以使

C
中,如果我使用
#include“someFile.h”
,预处理器将执行文本导入,这意味着
someFile.h
的内容将“复制并粘贴”到
#include
行上。在
C++
中,有
using
指令。这是否与
#include
的工作方式类似,即:名称空间的文本导入

using namespace std; // are the contents of std physically inserted on this line?

如果不是这样,那么
使用
指令是如何实现的。

不,它没有。这意味着从这一行开始,您可以使用
std
命名空间中的类和函数,而无需
std::
前缀。它不是
#include
的替代品。可悲的是,代码>仍然包含在C++中。

例如:

#include <iostream>

int main() {
  std::cout << "Hello "; // No `std::` would give compile error!
  using namespace std;
  cout << "world!\n"; // Now it's okay to use just `cout`.
  return 0;
}
#包括
int main(){

STD::CUT< P>不,它不是。这意味着,您可以从“代码> STD < /Cord>命名空间中使用类和函数,而不是<代码> STD::/Cord>前缀。它不是代码< >代码> <代码>的替代。遗憾的是,<代码>包含了 C++中仍然存在。

例如:

#include <iostream>

int main() {
  std::cout << "Hello "; // No `std::` would give compile error!
  using namespace std;
  cout << "world!\n"; // Now it's okay to use just `cout`.
  return 0;
}
#包括
int main(){

std::cout使用名称空间X的
将简单地告诉编译器“在查找名称时,查找X以及当前名称空间”。它不会“导入”任何内容。您可以在编译器中实际实现这一点,但效果不同“X中的所有符号都显示为在当前名称空间中可用”

或者换句话说,在搜索符号(以及搜索不带名称空间的名称本身)时,编译器似乎会在符号前面添加
X::

(如果你有一个符号<代码> x::a < /COD>和本地值<代码> A<代码>,或者我也使用< <代码> >命名空间y < /C>,我通常避免它,还有一个符号<代码> y::a < /c>。我确信C++标准确实说明了使用的,但是很容易用这样的结构来混淆自己和他人。s、 ]


一般来说,我在“所有内容”上使用显式命名空间限定符,因此我很少在自己的代码中使用
使用命名空间…

使用命名空间X的
只会告诉编译器“在查找名称时,查找X以及当前命名空间”。它不会“导入”在编译器中,有很多不同的方法可以实现这一点,但效果是“X中的所有符号都显示为在当前名称空间中可用”

或者换句话说,在搜索符号(以及搜索不带名称空间的名称本身)时,编译器似乎会在符号前面添加
X::

(如果你有一个符号<代码> x::a < /COD>和本地值<代码> A<代码>,或者我也使用< <代码> >命名空间y < /C>,我通常避免它,还有一个符号<代码> y::a < /c>。我确信C++标准确实说明了使用的,但是很容易用这样的结构来混淆自己和他人。s、 ]

一般来说,我对“所有内容”使用显式命名空间限定符,因此我很少在自己的代码中使用
使用命名空间…

没有任何内容是“导入的”使用
指令将
写入文件。它所做的只是提供较短的方法来写入命名空间中已存在的符号。例如,如果是文件的前两行,则通常不会编译以下内容:

#include <string>
static const string s("123");
using namespace std;
static const string s("123");
但当下列内容出现在文件顶部时,通常不会编译:

#include <string>
static const string s("123");
using namespace std;
static const string s("123");
这也不会:

using namespace std;
static const std::string s("123");
这是因为
使用namespace
实际上并不定义任何新符号;它需要一些其他代码(例如
标题中的代码)来定义这些符号

<>顺便说一下,很多人都会在任何代码中明智地告诉你不要使用命名空间STD < /C> >编写<代码>。你可以在C++中编程得很好,而不必为名字空间写“代码> >命名空间< /代码>。但是这是另一个在

< p>回答的问题。使用
指令将
写入文件。它所做的只是提供较短的方法来写入命名空间中已存在的符号。例如,如果是文件的前两行,则通常不会编译以下内容:

#include <string>
static const string s("123");
using namespace std;
static const string s("123");
但当下列内容出现在文件顶部时,通常不会编译:

#include <string>
static const string s("123");
using namespace std;
static const string s("123");
这也不会:

using namespace std;
static const std::string s("123");
这是因为
使用namespace
实际上并不定义任何新符号;它需要一些其他代码(例如
标题中的代码)来定义这些符号

<>顺便说一下,很多人都会在任何代码中明智地告诉你不要使用命名空间STD<代码>写C++。C++可以很好地编写C++程序,而不必为任何命名空间使用命名空间< /代码>编写代码。但是,这是另一个问题,在

不,<>代码>包含< <代码> >在C++中仍然是完全相同的。

要理解使用
,首先需要了解名称空间。这是避免大型C项目中发生符号冲突的一种方法,例如,在大型C项目中,很难保证两个第三方库不使用相同的名称定义函数。原则上,每个人都可以选择唯一的前缀,但我遇到过在实际项目中,非静态C链接器符号确实存在问题(我在看你,Oracle)

因此,
namespace
允许您对事物进行分组,包括整个库,包括标准库。它既避免了链接器冲突,也避免了对所获取函数版本的模糊性

例如,让我们创建一个几何图形库:

// geo.hpp
struct vector;
struct matrix;

int transform(matrix const &m, vector &v); // v -> m . v
并使用一些STL标题:

// vector
template <typename T, typename Alloc = std::allocator<T>> vector;

// algorithm
template <typename Input, typename Output, typename Unary>
void transform(Input, Input, Output, Unary);
// vector
namespace std {
    template <typename T, typename Alloc = std::allocator<T>> vector;
}

// algorithm
namespace std {
    template <typename Input, typename Output, typename Unary>
    void transform(Input, Input, Output, Unary);
}
并使用一些STL标题:

// vector
template <typename T, typename Alloc = std::allocator<T>> vector;

// algorithm
template <typename Input, typename Output, typename Unary>
void transform(Input, Input, Output, Unary);
// vector
namespace std {
    template <typename T, typename Alloc = std::allocator<T>> vector;
}

// algorithm
namespace std {
    template <typename Input, typename Output, typename Unary>
    void transform(Input, Input, Output, Unary);
}
这只会影响该函数范围内的符号。如果另一个函数选择注入
geo