Boost 的 C99 stdint 实现非常方便。不过,有一件事困扰着我。他们将所有的 typedef 转储到boost namespace
. 这让我在使用这个工具时有三个选择:
- 使用“
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
工作量少,我猜?