“std :: size_t”在C ++中有意义吗? [英] Does "std::size_t" make sense in C++?

查看:115
本文介绍了“std :: size_t”在C ++中有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我继承的一些代码中,我看到 size_t std 命名空间限定词频繁使用。例如:

  std :: size_t n = sizeof(long); 

当然,它编译和运行正常。但是对我来说似乎是不好的做法(可能从C继续。)



不是真的内置在C ++中,因此在全局命名空间?是一个头文件include需要在C ++中使用 size_t



另一种提出这个问题的方法是,

$

$

 $ <$ p> 

c $ c> size_t foo()
{
return sizeof(long);
}


解决方案

关于这个 $ :: size_t 的反向兼容性标题 stddef .h 。从它们的开始就是 ANSI / ISO C ISO C ++ 的一部分。每个C ++实现必须附带 stddef.h (兼容性)和 cstddef ,其中只有后者定义 std :: size_t ,而不一定是 :: size_t 。请参阅C ++标准的附录D.


In some code I've inherited, I see frequent use of size_t with the std namespace qualifier. For example:

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?).

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++?

Another way to ask this question is, would the following program (with no includes) be expected to compile on all C++ compilers?

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

解决方案

There seems to be confusion among the stackoverflow 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.

这篇关于“std :: size_t”在C ++中有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆