Portable C++ 03精确宽度类型

4 c++ portability types fixed-width

背景


不幸的是,目前的C++标准缺少stdint标题中定义的C99精确宽度类型.

我能找到(在便携性方面)的下一个最好的事情是Boostcstdint.hpp从实现Boost.Integer库.

关注


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

Boost的实现转储了所有的typedefs boost namesapce(而不是像boost::stdint).这完全是丑陋的,因为现在你被迫using只对你感兴趣的类型使用boost namespace-directive (这是一项额外的维护工作),或者把整个问题带到全球范围内(这会破坏namespaces 的观点)).当然,我可以boost::uint32_t在任何地方都是冗长和类型,但这也不是非常适合未来的².

问题


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

对于那些使用此标题或自己编辑的人,如何使用这些类型?盲目地合并boost namespace到全局namespace,前缀与一切" boost::"上撰文指出,包装了一个头Boost.Integercstdint.hpp,等等?

任何建议表示赞赏.

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

澄清


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

2 - 当标准的下一次迭代stdint.h进入时cstdint,我会遇到一堆带有" boost::" 前缀的代码.那么,这将是一个额外的依赖(即"boost/cstdint.hpp"),这将是完全无用的.

Cat*_*lus 6

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

它也很容易生成.使用这个小脚本,我明白了.您还可以添加要检查的定义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
Run Code Online (Sandbox Code Playgroud)