便携式 C++ 03 精确宽度类型

发布于 2024-08-05 09:35:24 字数 1101 浏览 6 评论 0原文

背景


不幸的是,当前的 C++ 标准缺少 stdint 标头中定义的 C99 精确宽度类型。

我能找到的下一个最好的东西(就可移植性而言)是 Boost.Integer 库中的 Boostcstdint.hpp 实现。

顾虑


也就是说,我遇到了一些问题:

Boost 的实现转储了 boost 命名空间 中的所有 typedef(而不是类似 boost::stdint 的东西)。这完全是丑陋的,因为现在您要么被迫仅在您感兴趣的类型上使用 using 指令(这是一项额外的维护工作),要么带来整个 >将命名空间提升到全局范围1(这违背了命名空间的要点)。当然,我可以冗长地在任何地方输入 boost::uint32_t ,但这也不是很适合未来。


我基本上是在寻求建议 尽可能透明地利用这些尚未标准(无论如何不在 C++ '03 中)类型的最佳方法是什么?

对于那些使用此标头或自己推出标头的人来说,如何使用这些类型?盲目地将 boost 命名空间合并到全局命名空间中,在所有内容上加上“boost::”前缀,编写一个包装 Boost 的标头。 Integercstdint.hpp 等?

任何建议表示赞赏。

最后,说了这么多(顺便说一句,这不是咆哮),我正在编写数学密集型代码,因此宽度保证对我来说很重要。

说明


1 - 当我编写将这些类型作为参数的函数/类模板时,全局作用域是我唯一的选择。

2 - 当标准的下一次迭代将 stdint.h 包装到 cstdint 中时,我将陷入一堆前缀为“boost::”的代码中”。那么,这将是一个完全无用的额外依赖项(即“boost/cstdint.hpp”)。

Background


Unfortunately the current C++ standard lacks C99's exact-width types defined in the stdint header.

The next best thing I could find (in terms of portability) was Boost's cstdint.hpp implementation from the Boost.Integer library.

Concerns


That said, I've got a few problems with it:

Boost's implementation dumps all the typedefs in the boost namesapce (instead of something like boost::stdint). This is totally ugly, because now you're either forced to use a using-directive only on the types that you're interested in (this is an extra maintenance chore), or bring the entire boost namespace into global¹ scope (this defeats the point of namespaces). I could, of course, be verbose and type boost::uint32_t everywhere, for example, but this isn't very future-friendly², either.

Questions


I'm basically looking for advice. What's the best way to go about utilizing these not-yet-standard (not in C++ '03, anyway) types as transparently as possible?

For those of you who use this header, or rolled your own, how do you use these types? Blindly merge the boost namespace into the global namespace, prefix everything with "boost::", wrote a header that wraps Boost.Integer's cstdint.hpp, etc.?

Any advice is appreciated.

Finally, having said all that (this wasn't a rant, by the way), I'm writing math-intensive code, so width-guarantees are important to me.

Clarifications


1 - Global scope is my only option when I'm writing functions / class templates that take these types as arguments.

2 - When the next iteration of the standard wraps stdint.h into cstdint, I'll be stuck with a bunch of code that's prefixed with "boost::". This, then, will be an extra dependency (i.e., "boost/cstdint.hpp") that will be totally useless.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋风の叶未落 2024-08-12 09:35:24

您可以只使用 stdint.h,并为没有它的编译器提供一个(例如对于 MSVC - msinttypes)。或者编写 使用所有 Boost 的 typedef 的 cstdint (它是一次写入,所以我认为维护不会有问题)。

它也很容易生成。使用这个小脚本,我明白了。您还可以添加定义来检查 int64

#ifndef GEN_CSTDINT
#define GEN_CSTDINT

#include <boost/cstdint.hpp>

using boost::int16_t;
using boost::int32_t;
using boost::int64_t;
using boost::int8_t;
using boost::int_fast16_t;
using boost::int_fast32_t;
using boost::int_fast64_t;
using boost::int_fast8_t;
using boost::int_least16_t;
using boost::int_least32_t;
using boost::int_least64_t;
using boost::int_least8_t;
using boost::intmax_t;
using boost::uint16_t;
using boost::uint32_t;
using boost::uint64_t;
using boost::uint8_t;
using boost::uint_fast16_t;
using boost::uint_fast32_t;
using boost::uint_fast64_t;
using boost::uint_fast8_t;
using boost::uint_least16_t;
using boost::uint_least32_t;
using boost::uint_least64_t;
using boost::uint_least8_t;
using boost::uintmax_t;

#endif // GEN_CSTDINT

You could just use stdint.h, and provide one for compilers that don't have it (e.g. for MSVC - msinttypes). Or write cstdint that is using all of Boost's typedefs (it's a write-once, so I don't think maintenance will be problematic).

It's easy to generate, too. Using this little script, i got that. You could also add defines to check for int64.

#ifndef GEN_CSTDINT
#define GEN_CSTDINT

#include <boost/cstdint.hpp>

using boost::int16_t;
using boost::int32_t;
using boost::int64_t;
using boost::int8_t;
using boost::int_fast16_t;
using boost::int_fast32_t;
using boost::int_fast64_t;
using boost::int_fast8_t;
using boost::int_least16_t;
using boost::int_least32_t;
using boost::int_least64_t;
using boost::int_least8_t;
using boost::intmax_t;
using boost::uint16_t;
using boost::uint32_t;
using boost::uint64_t;
using boost::uint8_t;
using boost::uint_fast16_t;
using boost::uint_fast32_t;
using boost::uint_fast64_t;
using boost::uint_fast8_t;
using boost::uint_least16_t;
using boost::uint_least32_t;
using boost::uint_least64_t;
using boost::uint_least8_t;
using boost::uintmax_t;

#endif // GEN_CSTDINT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文