gpt4 book ai didi

c++ - Boost 的 "cstdint"用法

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:05 25 4
gpt4 key购买 nike

Boost 的 C99 stdint 实现非常方便。不过有一件事让我很烦恼。他们将所有 typedef 转储到 boost namespace 中.在使用此工具时,这给我留下了三个选择:

  1. 使用“using namespace boost
  2. 使用“using boost::[u]<type><width>_t
  3. 使用 boost:: 明确引用目标类型字首;例如,boost::uint32_t foo = 0;

  • 选项 № 1 破坏了命名空间的意义。即使在本地范围内使用(例如,在函数内),函数参数之类的东西仍然必须像选项 3 一样添加前缀。
  • 选项 № 2 更好,但是有很多这样的类型,所以它会变得嘈杂。
  • 选项 3 增加了极大的噪音; boost::前缀通常≥所讨论类型的长度。

我的问题是:将所有这些类型引入全局命名空间的最优雅的方法是什么?我应该只写一个关于 boost/cstdint.hpp 的包装器吗?利用选项 № 2 并完成它?


此外,像这样包装 header 在 VC++ 10 上不起作用(标准库 header 的问题):

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

我猜是不是工作量减少了?

最佳答案

我只是使用 C99 的 stdint.h(它实际上现在在 VS 2010 中)。对于不包含它的 Visual C/C++ 版本,我使用了 MinGW 的公共(public)域版本,我修改它以使用 VC6(从我必须在 VC6 中工作时开始):

在这个 SO 问题中,您可能会考虑其他几个选项:C99 stdint.h header and MS Visual Studio

如果您想继续使用 boost/cstdint.hpp,我会说实现一个将类型带入全局命名空间的包装器 header 的建议将是可行的方法.

boost/cstdint.hpp 是否提供了 stdint.h 中没有的任何我应该知道的信息?

关于c++ - Boost 的 "cstdint"用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2709083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com