如何处理size_t与std :: size_t? [英] What to do with size_t vs. std::size_t?

查看:190
本文介绍了如何处理size_t与std :: size_t?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚阅读:

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

当您#include <cstddef>时,我意识到使用::size_t不符合标准(尽管我的编译器支持).我想遵守标准,但是我不想在所有size_t之前加上std::.那么,什么是更习惯/受欢迎的方式来处理此问题:

I realize using ::size_t is not standards-compliant (although supported by my compiler) when you #include <cstddef>. I want to comply with the standard, but I do not want to prepend std:: to all of my size_t's. So, what is the more customary/popular way to handle this:

  • using std::size_t;?
  • 包括<stddef.h>吗?
  • 仅依靠编译器支持吗?
  • 还有别的吗?
  • write using std::size_t;?
  • include <stddef.h>?
  • just rely on compiler support?
  • something else?

推荐答案

您应使用using指令进行指定.

You should specify it with the using directive.

using std::size_t;

将其添加到每个编译单元的全局范围,或者将其添加到本地范围(如果会在全局范围内造成干扰).

Add it either to the global scope of each compilation unit, or to the local scopes if it would cause interference at the global scope.

stddef.h也可以使用,说实话,该方法并不比该方法更糟糕".但是,stddef.h是一个向后兼容标头,最好避免在新代码中依赖它.

stddef.h also works, as you noted, and honestly that method is not "worse" than this one. However, stddef.h is a backwards compatibility header, and it might be best to avoid relying on it in new code.

我更喜欢using指令,因为它不会在您不需要的任何地方污染全局名称空间,并且不依赖于对非标准行为的任意编译器支持.此外,这是在可能有多个选项的情况下将类型带入命名空间的一种普遍接受的方法,因此它并不是size_t用法的唯一方法.

I prefer the using directive because it does not pollute the global namespace anywhere you don't need to, and does not rely on arbitrary compiler support of nonstandard behavior. Further, this is the generally accepted way to bring a type into a namespace when multiple options are otherwise possible, so it is not unique to the usage of size_t.

这不是一个人可以权威地回答的问题.我已经成为专业开发人员已有10年了,自1998年以来就开始使用C ++,但是我永远不会看到所编写的C ++总代码中有统计意义的重要部分.从我所看到的来看,有很多代码仍在使用stddef.h,并且它不会很快中断.

This isn't really something that a person can authoritatively answer. I've been a professional developer for 10 years, and have worked with C++ since 1998, but I will never see any statistically significant portion of the total C++ code that's been written. From what I have seen, there is plenty of code out there that still uses stddef.h, and it won't likely break anytime soon.

对于新代码,我更喜欢在各处键入"std ::"前缀,仅在麻烦或难以阅读时才使用using指令.但是,我认识到这对于继承"的代码可能会令人不快,这是使用指令的文件作用域更好的地方.如果您有时间适当地重构继承的代码,则有一个很好的论据,您应该这样做,但是很有可能涉及的不仅仅是size_t变量.

For new code, I prefer just typing the "std::" prefix everywhere, only applying the using directives when it becomes cumbersome or difficult to read. However, I recognize that this can be irritating for "inherited" code, which is where the file-scope using directives are better. If you have the time to properly refactor the inherited code, there's a good argument that you should do so, but it's very likely to involve more than just the size_t variables.

我还应该提到C ++常见问题解答(项目27.5)也提到了此问题此处,我给他们的印象是,他们大多建议与您团队中的其他人保持一致.

I should also mention that the C++ FAQ (item 27.5) mentions this concern as well here, where I got the impression they mostly recommend consistency with others on your team.

我想在这里指出,在文件范围内应用使用命名空间std"不是一个好习惯,尽管这也会将size_t带入全局命名空间.我会在此处链接原因.

I want to note here that it is NOT good practice to apply "using namespace std" at the file scope, though this would also bring size_t into the global namespace. I will link the reason for that here.

我似乎吓坏了tuple_cat(对不起),但我确实确实认为他的经验方法不错,所以我试图结合一些更改来解决他的疑虑.我尝试使用以下修改后的查询搜索github,但仍然可能会遇到一些问题:

I seem to have scared tuple_cat off (sorry), but I really did think his empirical method was good, so I'm trying to incorporate some changes to resolve my concerns with his answer. I tried searching github with the following modified queries, which admittedly still may have some issues:

A) "size_t" AND "stddef.h" language:c++
B) "std::size_t" AND "<cstddef>" language:c++
C) "size_t" AND "<cstddef>" AND NOT "std::size_t" language:c++
D) "size_t" AND "<cstddef>" AND "using namespace std" AND NOT "std::size_t" language:c++
E) "size_t" AND "<cstddef>" AND "using std::size_t" language:c++

我得到以下信息:

  • A)974,239个结果(采用stddef.h方法)
  • B)1,230,021结果(cstddef方法,带有"std ::"前缀)
  • C)469,721结果(cstddef方法,无前缀)
  • D)32,539个结果(cstddef方法,使用命名空间std",不要这样做!)
  • E)27,080个结果(我建议使用使用std :: size_t"方法)
  • A) 974,239 results (stddef.h approach)
  • B) 1,230,021 results (cstddef approach, with "std::" prefixes)
  • C) 469,721 results (cstddef approach, no prefixes)
  • D) 32,539 results (cstddef approach, "using namespace std", DON'T DO THIS!)
  • E) 27,080 results (method I recommend, "using std::size_t")

这绝对不是完美的,我欢迎提出批评以使其变得更好,但是如上所述,我推荐的方法似乎并不是最受欢迎的方法.根据数据,似乎最流行的方法是使用size_t(B)上的"std ::"前缀,然后加上"stddef.h"(A).幸运的是,(D)的糟糕方法并不流行,但是似乎很多人可能正在依赖其他文件/头将size_t引入全局名称空间,或者只是希望它已经在编译器(C)上.

It's definitely not perfect, and I welcome criticism to make it better, but it appears that the method I recommend, as stated, is not the most popular. Based on the data, it appears that the most popular is using the "std::" prefixes on size_t (B), followed by including "stddef.h" (A). Luckily, the bad approach of (D) is not popular, but it appears that many people may be relying on other files/headers to bring size_t into the global namespace, or just hoping it's already there on the compiler (C).

因此,要随大流",您应该在所有内容前加上"std ::".如果您不想这样做,那么"stddef.h"也很常用,但是我的首选仍然是using指令.

Therefore, to "go with the herd", you should prepend everything with "std::". If you don't want to do that, then "stddef.h" is also in very common use, but my preference is still the using directive.

这篇关于如何处理size_t与std :: size_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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