便携式 C++ 03 精确宽度类型
背景
不幸的是,当前的 C++ 标准缺少 stdint
标头中定义的 C99 精确宽度类型。
我能找到的下一个最好的东西(就可移植性而言)是 Boost.Integer
库中的 Boost
的 cstdint.hpp
实现。
顾虑
也就是说,我遇到了一些问题:
Boost
的实现转储了 boost 命名空间
中的所有 typedef
(而不是类似 boost::stdint
的东西)。这完全是丑陋的,因为现在您要么被迫仅在您感兴趣的类型上使用 using
指令(这是一项额外的维护工作),要么带来整个 >将命名空间
提升到全局范围1(这违背了命名空间
的要点)。当然,我可以冗长地在任何地方输入 boost::uint32_t
,但这也不是很适合未来。
。
我基本上是在寻求建议 尽可能透明地利用这些尚未标准(无论如何不在 C++ '03 中)类型的最佳方法是什么?
对于那些使用此标头或自己推出标头的人来说,如何使用这些类型?盲目地将 boost 命名空间
合并到全局命名空间
中,在所有内容上加上“boost::
”前缀,编写一个包装 Boost 的标头。 Integer
的 cstdint.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 typedef
s 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 namespace
s). 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
template
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以只使用 stdint.h,并为没有它的编译器提供一个(例如对于 MSVC - msinttypes)。或者编写
使用
所有 Boost 的 typedef 的 cstdint (它是一次写入,所以我认为维护不会有问题)。它也很容易生成。使用这个小脚本,我明白了。您还可以添加定义来检查
int64
。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
.您可以使用便携式版本的 stdint。
You could use portable version of stdint.