"std::size_t" 在 C++ 中有意义吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/237370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:58:25  来源:igfitidea点击:

Does "std::size_t" make sense in C++?

c++size-t

提问by jwfearn

In some code I've inherited, I see frequent use of size_twith the stdnamespace qualifier. For example:

在我继承的一些代码中,我看到经常使用size_tstd命名空间限定符一起使用。例如:

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_tis built into C++ and therefore in the global namespace? Is a header file include needed to use size_tin C++?

是不是size_t内置在 C++ 中并因此在全局命名空间中?是否需要size_t在 C++ 中使用头文件包含?

Another way to ask this question is, would the following program (with noincludes) be expected to compileon all C++ compilers?

问这个问题的另一种方法是,将下面的程序(有没有包括)可望汇编所有C ++编译器?

size_t foo()
{
    return sizeof( long );
}

回答by Johannes Schaub - litb

There seems to be confusion among the stackoverflow crowd concerning this

stackoverflow 人群中似乎对此感到困惑

::size_tis defined in the backward compatibility header stddef.h. It's been part of ANSI/ISO Cand ISO C++since their very beginning. Every C++ implementation has to ship with stddef.h(compatibility) and cstddefwhere only the latter defines std::size_tand 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。

回答by Don Wakefield

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 的标题中找到的项目,包括cstddef,它定义了 size_t。

So std::size_t is in fact correct.

所以 std::size_t 实际上是正确的。

回答by fizzer

You can get size_tin 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例如,您可以通过包含<stddef.h>而不是<cstddef>. 我看不到任何明显的好处,该功能已被弃用。

回答by ypnos

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 的一部分,如果您只使用诸如 之类的基本内容<cstdlib>,您可能最终会定义它。

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 是 size_t 的 C++ 等价物。正如 Brian 指出的那样, std:: 用作命名空间以避免设置不适合所有人的全局变量。它就像 std::string 一样,它也可以在根命名空间中定义。

回答by mloskot

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++ 标准声明头文件的内容与 C 标准库中的相应头文件相同或基于相应的头文件,但在许多情况下,已应用更改。换句话说,它不是将 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?

size_t foo()
{
    return sizeof( long );
}

下面的程序(没有包含)应该在所有 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)。AC ++ 翻译单元 (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 但不包括标头的程序格式不正确。

回答by Martin

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 包含类似

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

我认为这应该说清楚。只要包含 <cstddef> ,就可以使用 size_t 或 std::size_t ,因为 size_t 在 std 命名空间之外进行了类型定义,然后被包含在内。有效地你有

typedef long int ptrdiff_t;
typedef unsigned long int size_t;

namespace std {

  using ::ptrdiff_t;
  using ::size_t;

}

回答by Martin

I think the clarifications are clear enough. The std::size_tmakes good sense in C++ and ::size_tmake (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_tand std::size_tare compatible?

然而,一个问题仍然存在。即假设::size_tstd::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:

我认为很多人都在使用某种东西:

----
// 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_twhile 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会在源文件中使用while 将使用std::size_t. 所以它们必须兼容,对吧?否则你会得到一个编译器错误。

/Michael S.

/迈克尔·S。

回答by Brian R. Bondy

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。例如升压。std::size_t 指定您肯定需要 C++ 标准。

size_t is a c++ standard type and it is defined within the namespace std.

size_t 是 C++ 标准类型,它在命名空间 std 中定义。