Boost的C99 stdint实现非常方便。但有一件事让我感到不安。他们将所有的typedefs转储到boost namespace
。使用此工具时,这给我留下了三个选项:Boost的“cstdint”用法
- 使用“
using namespace boost
” - 使用“
using boost::[u]<type><width>_t
” - 明确提及与
boost::
前缀的目标类型;例如,boost::uint32_t foo = 0;
- 选项№1种失败的命名空间的点。即使在局部范围内使用(例如,在一个函数内),像函数参数这样的东西仍然必须以选项№3作为前缀。
- 选项№2更好,但有一堆这些类型,所以它可以得到嘈杂。
- 选项№3增加了一个极端的噪音水平;
boost::
前缀通常≥所讨论类型的长度。
我的问题是:什么是最优雅的方式将所有这些类型的成全局命名空间?我应该只写一个包装boost/cstdint.hpp
的包装,利用选项№2并完成它?
而且,包裹头部,像这样没有对VC++ 10(问题与标准库头)工作:
namespace Foo
{
#include <boost/cstdint.hpp>
namespace boost_alias = boost;
}
using namespace Foo::boost_alias;
编辑:我想另一种选择是使用预处理器让它在VC 10上工作?以上面的片段:
#ifndef FOO_HPP_INCLUDED
#define FOO_HPP_INCLUDED
#if _MSC_VER >= 1600 /*VC++ 10*/ || defined USE_NATIVE_STDINT_HEADER
#include <stdint.h>
#else
namespace cstdint_wrapper
{
#include <boost/cstdint.hpp>
namespace boost_alias = boost;
}
using namespace cstdint_wrapper::boost_alias;
#endif
#endif
工作量减少了,我猜?
可能的重复http://stackoverflow.com/questions/1481733/portable-c-03-exact-width-types – 2010-04-25 17:21:07
@gf:漂亮的脚本,谢谢。 – patt0h 2010-04-25 17:43:59
为什么你需要*所有这些类型?我经常使用其中的一个或两个,但我无法想起需要所有这些。 – jalf 2010-04-25 18:27:25