-->

便携式C ++ 03精确宽度类型(Portable C++ 03 Exact Width Types

2019-07-19 17:27发布

背景


不幸的是,当前的C ++标准缺乏C99的在所定义的确切宽类型stdint报头。

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

关注


这就是说,我有与它的几个问题:

Boost的实施转储所有typedef S IN的boost namesapce (而不是像boost::stdint )。 这完全是丑陋的,因为现在你要么被迫使用using -directive只对你感兴趣的类型(这是一个额外的维护家务),或使整个boost namespace为global¹范围(这违背了点namespace S)。 我当然可以,是冗长和类型boost::uint32_t无处不在,例如,但这还不是很未来friendly²,无论是。

问题


基本上,我在寻找的建议。 什么是尽可能透明去利用这些还未标准(而不是在C ++ '03,反正)类型尽可能最好的方式是什么?

对于那些你们谁用这个头,或推出自己的,你怎么使用这些类型? 盲目合并boost namespace到全局namespace ,前面加上“一切boost:: ”上撰文指出,包装了一个头Boost.Integercstdint.hpp等?

任何建议表示赞赏。

最后,说了这么多(这不是夸大其词,顺便说一句),我写数学密集型代码,所以宽度保证对我很重要。

澄清


1 -全球范围内是我唯一的选择,当我写的函数/ class template s表示采取这些类型作为参数。

2 -当标准包装的下一次迭代stdint.hcstdint ,我会坚持用一串代码,与前缀“ boost:: ”。 那么,这将是一个额外的依赖(即“升压/ cstdint.hpp”),这将是完全无用的。

Answer 1:

你可以只使用stdint.h,并提供一个没有它的编译器(如MSVC - msinttypes )。 或写入时cstdint using所有Boost的类型定义(这是一个只写一次,所以我不认为维护将是有问题的)。

这很容易产生了。 使用这个脚本 ,我明白了。 您还可以添加定义来检查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


Answer 2:

你可以使用stdint的便携版本 。



文章来源: Portable C++ 03 Exact Width Types