在 C++ 中使用 C 头文件时,我们应该使用来自 std:: 还是全局命名空间的函数?
When using C headers in C++, should we use functions from std:: or the global namespace?
C 在某种程度上(不完全是)C++ 的子集。所以我们可以通过稍微改变名称(stdio.h
到cstdio
,stdlib.h
到cstdlib
)来使用C++中的大部分C functions/headers。
我的问题实际上是一种语义问题。在 C++ 代码中(使用最新版本的 GCC 编译器),我可以调用 printf("Hello world!");
和 std::printf("Hello world!");
,它们的工作原理完全相同。在我使用的参考中它也显示为 std::printf("Hello world!");
.
我的问题是,在 C++ 中更喜欢使用 std::printf();
吗?有区别吗?
仅使用 printf
而不使用 std::
可能会产生一些名称冲突,许多 C++ 开发人员认为这是一种不好的做法。 Google 是你的朋友,但这里有一些链接,希望这对你有帮助
Why is "using namespace std" considered bad practice?
http://www.cplusplus.com/forum/beginner/61121/
<cmeow>
始终提供 ::std::purr
,可能提供也可能不提供 ::purr
。
<meow.h>
始终提供 ::purr
,可能会也可能不会提供 ::std::purr
。
使用保证由您包含的 header 提供的表格。
唯一的区别是,在 std::printf()
中,通过添加 std::
范围解析,您可以防止将来有人编写同名函数,这会导致命名空间冲突.这两种用法将导致完全相同的 OS API 调用(您可以在 Linux by 运行 strace your_program
下查看)。
我发现很少有人会这样命名一个函数,因为 printf()
是那里最常用的函数之一。此外,在 C++ 中,iostream
优先于对 cstdio
函数(如 printf.
的调用
来自 C++11 标准:
Every C header, each of which has a name of the form name.h, behaves
as if each name placed in the standard library namespace by the
corresponding cname header is placed within the global namespace
scope. It is unspecified whether these names are first declared or
defined within namespace scope (3.3.6) of the namespace std and are
then injected into the global namespace scope by explicit
using-declarations (7.3.3).
因此,如果您使用 <cstdio>
,您可以确定 printf
将在 namespace std
中,因此不在全局命名空间中。
使用全局名称空间会产生名称冲突。 这不是 C++ 方式。
因此,我正在使用 <cstdio>
headers 并建议您这样做。
不,无论哪种方式都可以。
最初 的意图是 <___.h>
头文件将是 C 版本,它将所有内容都放在全局名称空间中,而 <c___>
头文件将是C++ 化版本,将所有内容都放在 std
命名空间中。
但实际上,C++ 版本也 将所有内容都放入了全局名称空间。并且没有明确的共识,即使用 std::
版本是 "the right thing to do".
所以基本上,使用您喜欢的任何一个。最常见的可能是在全局命名空间中使用 C 标准库函数(printf
而不是 std::printf
),但没有太多理由考虑一个 "better" 而不是另一个。
来自 C++11 标准(强调我的):
D.5 C standard library headers [depr.c.headers]
- For compatibility with the C standard library ...
- Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard
library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).
- Example: The header
<cstdlib>
assuredly provides its declarations and definitions within the namespace
std
. It may also provide these names within the global namespace. The header <stdlib.h>
assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It
may also provide these names within the namespace std
.
使用 «name.h» headers 已被弃用,它们已被确定为从未来修订中删除的候选对象。
因此,我建议包含 «cname» headers 并使用来自 std
命名空间的声明和定义。
如果您出于某些原因必须使用 «name.h» headers(已弃用,见上文),我建议使用全局命名空间中的声明和定义。
换句话说:更喜欢
#include <cstdio>
int main() {
std::printf("Hello world\n");
}
超过
#include <stdio.h>
int main() {
printf("Hello world\n");
}
在标准输入输出中
This is the C++ version of the Standard C Library header @c stdio.h,
and its contents are (mostly) the same as that header, but are all
contained in the namespace @c std (except for names which are defined
as macros in C).
所以应该没什么区别。
根据我自己的做法:使用 std::
前缀。否则有一天abs
会咬你一口,万一你用了浮点数
非限定abs
是指在某些平台上int
上定义的函数。在其他人身上它超载了。但是 std::abs
总是对所有类型重载。
C 在某种程度上(不完全是)C++ 的子集。所以我们可以通过稍微改变名称(stdio.h
到cstdio
,stdlib.h
到cstdlib
)来使用C++中的大部分C functions/headers。
我的问题实际上是一种语义问题。在 C++ 代码中(使用最新版本的 GCC 编译器),我可以调用 printf("Hello world!");
和 std::printf("Hello world!");
,它们的工作原理完全相同。在我使用的参考中它也显示为 std::printf("Hello world!");
.
我的问题是,在 C++ 中更喜欢使用 std::printf();
吗?有区别吗?
仅使用 printf
而不使用 std::
可能会产生一些名称冲突,许多 C++ 开发人员认为这是一种不好的做法。 Google 是你的朋友,但这里有一些链接,希望这对你有帮助
Why is "using namespace std" considered bad practice? http://www.cplusplus.com/forum/beginner/61121/
<cmeow>
始终提供 ::std::purr
,可能提供也可能不提供 ::purr
。
<meow.h>
始终提供 ::purr
,可能会也可能不会提供 ::std::purr
。
使用保证由您包含的 header 提供的表格。
唯一的区别是,在 std::printf()
中,通过添加 std::
范围解析,您可以防止将来有人编写同名函数,这会导致命名空间冲突.这两种用法将导致完全相同的 OS API 调用(您可以在 Linux by 运行 strace your_program
下查看)。
我发现很少有人会这样命名一个函数,因为 printf()
是那里最常用的函数之一。此外,在 C++ 中,iostream
优先于对 cstdio
函数(如 printf.
来自 C++11 标准:
Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).
因此,如果您使用 <cstdio>
,您可以确定 printf
将在 namespace std
中,因此不在全局命名空间中。
使用全局名称空间会产生名称冲突。 这不是 C++ 方式。
因此,我正在使用 <cstdio>
headers 并建议您这样做。
不,无论哪种方式都可以。
最初 的意图是 <___.h>
头文件将是 C 版本,它将所有内容都放在全局名称空间中,而 <c___>
头文件将是C++ 化版本,将所有内容都放在 std
命名空间中。
但实际上,C++ 版本也 将所有内容都放入了全局名称空间。并且没有明确的共识,即使用 std::
版本是 "the right thing to do".
所以基本上,使用您喜欢的任何一个。最常见的可能是在全局命名空间中使用 C 标准库函数(printf
而不是 std::printf
),但没有太多理由考虑一个 "better" 而不是另一个。
来自 C++11 标准(强调我的):
D.5 C standard library headers [depr.c.headers]
- For compatibility with the C standard library ...
- Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).
- Example: The header
<cstdlib>
assuredly provides its declarations and definitions within the namespacestd
. It may also provide these names within the global namespace. The header<stdlib.h>
assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespacestd
.
使用 «name.h» headers 已被弃用,它们已被确定为从未来修订中删除的候选对象。
因此,我建议包含 «cname» headers 并使用来自 std
命名空间的声明和定义。
如果您出于某些原因必须使用 «name.h» headers(已弃用,见上文),我建议使用全局命名空间中的声明和定义。
换句话说:更喜欢
#include <cstdio>
int main() {
std::printf("Hello world\n");
}
超过
#include <stdio.h>
int main() {
printf("Hello world\n");
}
在标准输入输出中
This is the C++ version of the Standard C Library header @c stdio.h, and its contents are (mostly) the same as that header, but are all contained in the namespace @c std (except for names which are defined as macros in C).
所以应该没什么区别。
根据我自己的做法:使用 std::
前缀。否则有一天abs
会咬你一口,万一你用了浮点数
非限定abs
是指在某些平台上int
上定义的函数。在其他人身上它超载了。但是 std::abs
总是对所有类型重载。