4

背景


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

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

关注点


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

Boost的实现转储所有的typedefs boost namesapce(而不是类似的东西boost::stdint)。这非常难看,因为现在您要么被迫using仅对您感兴趣的类型使用 -directive(这是一项额外的维护工作),要么将整个boost namespace指令带入全局¹范围(这违背了namespaces的观点)。例如,我当然可以很冗长,boost::uint32_t到处打字,但这也不是很适合未来的²。

问题


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

对于那些使用此标头或滚动您自己的标头的人,您如何使用这些类型?盲目地把 's 合并boost namespace到全局namespace中,用“”前缀所有的东西boost::,写一个包装Boost.Integer's的标题cstdint.hpp,等等?

任何建议表示赞赏。

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

澄清


1 - 当我编写class template将这些类型作为参数的函数/s 时,全局范围是我唯一的选择。

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

4

2 回答 2

6

您可以只使用 stdint.h,并为没有它的编译器提供一个(例如,对于 MSVC - msinttypes)。或者编写usingBoost 的所有 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
于 2009-09-26T18:03:08.913 回答
1

您可以使用便携式版本的 stdint

于 2009-09-26T17:57:32.213 回答