In some code I've inherited, I see frequent use of size_t
with the std
namespace qualifier. For example:
在我继承的一些代码中,我看到了在std名称空间限定符中经常使用size_t。例如:
std::size_t n = sizeof( long );
It compiles and runs fine, of course. But it seems like bad practice to me (perhaps carried over from C?).
当然,它编译并运行良好。但对我来说,这似乎是一种糟糕的做法(可能是由C带来的)。
Isn't it true that size_t
is built into C++ and therefore in the global namespace? Is a header file include needed to use size_t
in C++?
size_t被构建到c++中,因此是在全局命名空间中,这不是真的吗?在c++中使用size_t是否需要一个头文件?
Another way to ask this question is, would the following program (with no includes) be expected to compile on all C++ compilers?
另一种问这个问题的方法是,下面的程序(不包含)会在所有c++编译器上编译吗?
size_t foo()
{
return sizeof( long );
}
8 个解决方案
#1
127
There seems to be confusion among the * crowd concerning this
关于这一点,斯塔克溢出的人群中似乎存在着困惑
::size_t
is defined in the backward compatibility header stddef.h
. It's been part of ANSI/ISO C
and ISO C++
since their very beginning. Every C++ implementation has to ship with stddef.h
(compatibility) and cstddef
where only the latter defines std::size_t
and not necessarily ::size_t
. See Annex D of the C++ Standard.
::size_t是在向后兼容头stddef中定义的。h。它从一开始就是ANSI/ISO C和ISO c++的一部分。每个c++实现都必须附带stddef。h(兼容性)和cstddef中只有后者定义std::size_t,而不一定是::size_t。见c++标准附件D。
#2
41
Section 17.4.1.2 of the C++ standard, paragraph 4, states that:
c++标准第17.4.1.2节第4段规定:
"In the C++ Standard Library, however, the declarations and definitions (except for names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std."
但是,在c++标准库中,声明和定义(在C中定义为宏的名称除外)位于名称空间std的命名空间范围内(3.3.5)。
This includes items found in headers of the pattern cname, including cstddef, which defines size_t.
这包括模式cname头中的项,包括定义size_t的cstddef。
So std::size_t is in fact correct.
所以std::size_t实际上是正确的。
#3
13
You can get size_t
in the global namespace by including, for example, <stddef.h>
instead of <cstddef>
. I can't see any obvious benefit, and the feature is deprecated.
您可以在全局命名空间中获得size_t,例如,
#4
4
size_t is not built into C++. And it is not defined by default. This one doesn't compile with GCC:
size_t没有构建到c++中。它不是默认定义的。这个没有使用GCC来编译:
int main(int argc, char** argv) {
size_t size;
}
That said, size_t is part of POSIX and if you use only basic things like <cstdlib>
, you will likely end up having it defined.
就是说,size_t是POSIX的一部分,如果您只使用
You could argue that std::size_t is the C++ equivalent of size_t. As Brian pointed out, std:: is used as namespace to avoid setting global variables which don't fit everybody. It's just like std::string, which could also have been defined in the root namespace.
您可能会认为std::size_t是c++中的size_t。正如Brian所指出的,std::被用作命名空间,以避免设置不适合所有人的全局变量。它就像std::string,它也可以在根命名空间中定义。
#5
4
std::size_t n = sizeof( long );
Actually, you haven't asked what specifically seems to be a bad practice int the above. Use of size_t, qualification with std namespace,...
事实上,你还没有问过在上面的例子中,有什么是不好的。使用size_t,限定与std命名空间,…
As the C++ Standard says (18.1), size_t is a type defined in the standard header . I'd suggest to drop any thoughts and impressions about possible inheritance from C language. C++ is a separate and different language and it's better to consider it as such. It has its own standard library and all elements of C++ Standard Library are defined within namespace std. However, it is possible to use elements of C Standard Library in C++ program.
正如c++标准所说(18.1),size_t是标准头中定义的类型。我建议放弃任何关于C语言可能继承的想法和印象。c++是一种独立的、不同的语言,最好把它看作是独立的。它有自己的标准库,c++标准库的所有元素都是在命名空间std中定义的,但是在c++程序中可以使用C标准库的元素。
I'd consider including as a dirty hack. The C++ Standard states that the content of headers is the same or based on corresponding headers from the C Standard Library, but in number of cases, changes have been applied. In other words, it's not a direct copy & paste of C headers into C++ headers.
我认为包括在内是一种卑鄙的手段。c++标准声明header的内容是相同的,或者基于来自C标准库的相应header,但是在很多情况下,已经应用了更改。换句话说,它不是将C头文件直接复制粘贴到c++头文件中。
size_t is not a built-in type in C++. It is a type defined to specify what kind of integral type is used as a return type of sizeof() operator, because an actual return type of sizeof() is implementation defined, so the C++ Standard unifies by defining size_t.
size_t在c++中不是内置类型。它定义了一种类型,用于指定将哪种积分类型用作sizeof()运算符的返回类型,因为实现定义了实际的sizeof()返回类型,因此c++标准通过定义size_t进行统一。
would the following program (with no includes) be expected to compile on all C++ compilers?
是否期望在所有c++编译器上编译以下程序(不包含)?
size_t foo() { return sizeof( long ); }
The C++ Standard says (1.4):
c++标准说(1.4):
The names defined in the library have namespace scope (7.3). A C ++ translation unit (2.1) obtains access to these names by including the appropriate standard library header (16.2).
库中定义的名称具有名称空间范围(7.3)。一个c++翻译单元(2.1)通过包含适当的标准库头(16.2)获得对这些名称的访问权。
The size_t is a name defined within std namespace, so every program that uses this name should include corresponding header, in this case.
size_t是在std命名空间中定义的一个名称,因此在本例中,每个使用这个名称的程序都应该包含相应的标题。
Next, the 3.7.3 chapter says:
接下来,3.7.3章说:
However, referring to std, std::bad_alloc, and std::size_t is ill-formed unless the name has been declared by including the appropriate header.
但是,引用std、std: bad_alloc和std: size_t是不合适的,除非通过包含适当的头声明名称。
Given that, program using size_t but not including header is ill-formed.
考虑到这一点,使用size_t而不包括header的程序是错误的。
#6
2
Sometimes other libraries will define their own size_t. For example boost. std::size_t specifies that you definitely want the c++ standard one.
有时其他库会定义自己的size_t。例如提高。size_t指定您肯定想要c++标准。
size_t is a c++ standard type and it is defined within the namespace std.
size_t是一个c++标准类型,它是在命名空间std中定义的。
#7
2
I think the clarifications are clear enough. The std::size_t
makes good sense in C++ and ::size_t
make (at least) good sense in C.
我认为澄清已经足够清楚了。std::size_t在c++和::size_t(至少)在C中很有意义。
However a question remain. Namely whether it is safe to assume that ::size_t
and std::size_t
are compatible?
然而一个问题依然存在。即:size_t和std::size_t是否兼容?
From a pure typesafe perspective they are not necessarily identical unless it is defined somewhere that they must be identical.
从纯粹类型的角度来看,它们并不一定是相同的,除非在某个地方定义它们必须是相同的。
I think many are using something a la:
我想很多人都在使用la:
----
// a.hpp
#include <string>
void Foo( const std::string & name, size_t value );
-----
// a.cpp
#include "a.hpp"
using namespace std;
void Foo( const string & name, size_t value )
{
...
}
So in the header you defintely use the ::size_t
while in the source file you'll use std::size_t
. So they must be compatible, right? Otherwise you'll get a compiler error.
因此,在头文件中,您肯定会使用:size_t,而在源文件中,您将使用std::size_t。所以它们必须是相容的,对吧?否则会出现编译错误。
/Michael S.
/迈克尔·S。
#8
1
The GNU compiler headers contain something like
GNU编译器头包含类似的内容
typedef long int __PTRDIFF_TYPE__; typedef unsigned long int __SIZE_TYPE__;
Then stddef.h constains something like
然后stddef。h constains之类的
typedef __PTRDIFF_TYPE__ ptrdiff_t; typedef __SIZE_TYPE__ size_t;
And finally the cstddef file contains something like
最后,cstddef文件包含类似的内容
#include <stddef.h> namespace std { using ::ptrdiff_t; using ::size_t; }
I think that should make it clear. As long as you include <cstddef> you can use either size_t or std::size_t because size_t was typedefed outside the std namespace and was then included. Effectively you have
我想这应该说得很清楚。只要包含
typedef long int ptrdiff_t; typedef unsigned long int size_t; namespace std { using ::ptrdiff_t; using ::size_t; }
#1
127
There seems to be confusion among the * crowd concerning this
关于这一点,斯塔克溢出的人群中似乎存在着困惑
::size_t
is defined in the backward compatibility header stddef.h
. It's been part of ANSI/ISO C
and ISO C++
since their very beginning. Every C++ implementation has to ship with stddef.h
(compatibility) and cstddef
where only the latter defines std::size_t
and not necessarily ::size_t
. See Annex D of the C++ Standard.
::size_t是在向后兼容头stddef中定义的。h。它从一开始就是ANSI/ISO C和ISO c++的一部分。每个c++实现都必须附带stddef。h(兼容性)和cstddef中只有后者定义std::size_t,而不一定是::size_t。见c++标准附件D。
#2
41
Section 17.4.1.2 of the C++ standard, paragraph 4, states that:
c++标准第17.4.1.2节第4段规定:
"In the C++ Standard Library, however, the declarations and definitions (except for names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std."
但是,在c++标准库中,声明和定义(在C中定义为宏的名称除外)位于名称空间std的命名空间范围内(3.3.5)。
This includes items found in headers of the pattern cname, including cstddef, which defines size_t.
这包括模式cname头中的项,包括定义size_t的cstddef。
So std::size_t is in fact correct.
所以std::size_t实际上是正确的。
#3
13
You can get size_t
in the global namespace by including, for example, <stddef.h>
instead of <cstddef>
. I can't see any obvious benefit, and the feature is deprecated.
您可以在全局命名空间中获得size_t,例如,
#4
4
size_t is not built into C++. And it is not defined by default. This one doesn't compile with GCC:
size_t没有构建到c++中。它不是默认定义的。这个没有使用GCC来编译:
int main(int argc, char** argv) {
size_t size;
}
That said, size_t is part of POSIX and if you use only basic things like <cstdlib>
, you will likely end up having it defined.
就是说,size_t是POSIX的一部分,如果您只使用
You could argue that std::size_t is the C++ equivalent of size_t. As Brian pointed out, std:: is used as namespace to avoid setting global variables which don't fit everybody. It's just like std::string, which could also have been defined in the root namespace.
您可能会认为std::size_t是c++中的size_t。正如Brian所指出的,std::被用作命名空间,以避免设置不适合所有人的全局变量。它就像std::string,它也可以在根命名空间中定义。
#5
4
std::size_t n = sizeof( long );
Actually, you haven't asked what specifically seems to be a bad practice int the above. Use of size_t, qualification with std namespace,...
事实上,你还没有问过在上面的例子中,有什么是不好的。使用size_t,限定与std命名空间,…
As the C++ Standard says (18.1), size_t is a type defined in the standard header . I'd suggest to drop any thoughts and impressions about possible inheritance from C language. C++ is a separate and different language and it's better to consider it as such. It has its own standard library and all elements of C++ Standard Library are defined within namespace std. However, it is possible to use elements of C Standard Library in C++ program.
正如c++标准所说(18.1),size_t是标准头中定义的类型。我建议放弃任何关于C语言可能继承的想法和印象。c++是一种独立的、不同的语言,最好把它看作是独立的。它有自己的标准库,c++标准库的所有元素都是在命名空间std中定义的,但是在c++程序中可以使用C标准库的元素。
I'd consider including as a dirty hack. The C++ Standard states that the content of headers is the same or based on corresponding headers from the C Standard Library, but in number of cases, changes have been applied. In other words, it's not a direct copy & paste of C headers into C++ headers.
我认为包括在内是一种卑鄙的手段。c++标准声明header的内容是相同的,或者基于来自C标准库的相应header,但是在很多情况下,已经应用了更改。换句话说,它不是将C头文件直接复制粘贴到c++头文件中。
size_t is not a built-in type in C++. It is a type defined to specify what kind of integral type is used as a return type of sizeof() operator, because an actual return type of sizeof() is implementation defined, so the C++ Standard unifies by defining size_t.
size_t在c++中不是内置类型。它定义了一种类型,用于指定将哪种积分类型用作sizeof()运算符的返回类型,因为实现定义了实际的sizeof()返回类型,因此c++标准通过定义size_t进行统一。
would the following program (with no includes) be expected to compile on all C++ compilers?
是否期望在所有c++编译器上编译以下程序(不包含)?
size_t foo() { return sizeof( long ); }
The C++ Standard says (1.4):
c++标准说(1.4):
The names defined in the library have namespace scope (7.3). A C ++ translation unit (2.1) obtains access to these names by including the appropriate standard library header (16.2).
库中定义的名称具有名称空间范围(7.3)。一个c++翻译单元(2.1)通过包含适当的标准库头(16.2)获得对这些名称的访问权。
The size_t is a name defined within std namespace, so every program that uses this name should include corresponding header, in this case.
size_t是在std命名空间中定义的一个名称,因此在本例中,每个使用这个名称的程序都应该包含相应的标题。
Next, the 3.7.3 chapter says:
接下来,3.7.3章说:
However, referring to std, std::bad_alloc, and std::size_t is ill-formed unless the name has been declared by including the appropriate header.
但是,引用std、std: bad_alloc和std: size_t是不合适的,除非通过包含适当的头声明名称。
Given that, program using size_t but not including header is ill-formed.
考虑到这一点,使用size_t而不包括header的程序是错误的。
#6
2
Sometimes other libraries will define their own size_t. For example boost. std::size_t specifies that you definitely want the c++ standard one.
有时其他库会定义自己的size_t。例如提高。size_t指定您肯定想要c++标准。
size_t is a c++ standard type and it is defined within the namespace std.
size_t是一个c++标准类型,它是在命名空间std中定义的。
#7
2
I think the clarifications are clear enough. The std::size_t
makes good sense in C++ and ::size_t
make (at least) good sense in C.
我认为澄清已经足够清楚了。std::size_t在c++和::size_t(至少)在C中很有意义。
However a question remain. Namely whether it is safe to assume that ::size_t
and std::size_t
are compatible?
然而一个问题依然存在。即:size_t和std::size_t是否兼容?
From a pure typesafe perspective they are not necessarily identical unless it is defined somewhere that they must be identical.
从纯粹类型的角度来看,它们并不一定是相同的,除非在某个地方定义它们必须是相同的。
I think many are using something a la:
我想很多人都在使用la:
----
// a.hpp
#include <string>
void Foo( const std::string & name, size_t value );
-----
// a.cpp
#include "a.hpp"
using namespace std;
void Foo( const string & name, size_t value )
{
...
}
So in the header you defintely use the ::size_t
while in the source file you'll use std::size_t
. So they must be compatible, right? Otherwise you'll get a compiler error.
因此,在头文件中,您肯定会使用:size_t,而在源文件中,您将使用std::size_t。所以它们必须是相容的,对吧?否则会出现编译错误。
/Michael S.
/迈克尔·S。
#8
1
The GNU compiler headers contain something like
GNU编译器头包含类似的内容
typedef long int __PTRDIFF_TYPE__; typedef unsigned long int __SIZE_TYPE__;
Then stddef.h constains something like
然后stddef。h constains之类的
typedef __PTRDIFF_TYPE__ ptrdiff_t; typedef __SIZE_TYPE__ size_t;
And finally the cstddef file contains something like
最后,cstddef文件包含类似的内容
#include <stddef.h> namespace std { using ::ptrdiff_t; using ::size_t; }
I think that should make it clear. As long as you include <cstddef> you can use either size_t or std::size_t because size_t was typedefed outside the std namespace and was then included. Effectively you have
我想这应该说得很清楚。只要包含
typedef long int ptrdiff_t; typedef unsigned long int size_t; namespace std { using ::ptrdiff_t; using ::size_t; }