C++ Boost Utility超详细讲解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++ Boost Utility超详细讲解

无水先生   2022-12-08 我要评论

一、说明

Boost.Utility 库是杂项、有用的类和函数的集合,它们太小而无法在独立库中维护。虽然实用程序很小并且可以快速学习,但它们完全无关。与其他章节中的示例不同,此处的代码示例不是相互构建的,因为它们是独立的实用程序。

虽然大多数实用程序都在 boost/utility.hpp 中定义,但有些实用程序有自己的头文件。以下示例包括所介绍的实用程序的相应头文件。

二、Boost.Utility库示例和代码

示例 69.1。使用 boost::checked_delete()

#include <boost/checked_delete.hpp>
#include <boost/intrusive/list.hpp>
#include <string>
#include <utility>
#include <iostream>
struct animal : public boost::intrusive::list_base_hook<>
{
  std::string name_;
  int legs_;
 
  animal(std::string name, int legs) : name_{std::move(name)},
    legs_{legs} {}
};
int main()
{
  animal *a = new animal{"cat", 4};
  typedef boost::intrusive::list<animal> animal_list;
  animal_list al;
  al.push_back(*a);
  al.pop_back_and_dispose(boost::checked_delete<animal>);
  std::cout << al.size() << '\n';
}

Example 69.1

示例 69.1 将函数 boost::checked_delete() 作为参数传递给成员函数 pop_back_and_dispose(),该函数由 Boost.Intrusive 的类 boost::intrusive::list 提供。 boost::intrusive::list 和 pop_back_and_dispose() 在第 18 章中介绍,而 boost::checked_delete() 由 Boost.Utility 提供并在 boost/checked_delete.hpp 中定义。

boost::checked_delete() 期望作为其唯一参数的指针指向将被 delete 删除的对象。因为 pop_back_and_dispose() 需要一个函数来销毁相应的对象,所以传入 boost::checked_delete() 是有意义的——这样,您就不需要定义类似的函数。

与 delete 不同,boost::checked_delete() 确保要销毁的对象的类型是完整的。 delete 将接受指向具有不完整类型的对象的指针。虽然这涉及您通常可以忽略的 C++ 标准细节,但您应该注意 boost::checked_delete() 与调用 delete 并不完全相同,因为它对其参数提出了更高的要求。

Boost.Utility 还提供了 boost::checked_array_delete(),可用于销毁数组。它调用 delete[] 而不是 delete。

此外,boost::checked_deleter 和 boost::checked_array_deleter 这两个类可用于创建行为分别类似于 boost::checked_delete() 和 boost::checked_array_delete() 的函数对象。

示例 69.2。使用 BOOST_CURRENT_FUNCTION

#include <boost/current_function.hpp>
#include <iostream>
int main()
{
  const char *funcname = BOOST_CURRENT_FUNCTION;
  std::cout << funcname << '\n';
}

Example 69.2

示例 69.2 使用在 boost/current_function.hpp 中定义的宏 BOOST_CURRENT_FUNCTION 将周围函数的名称作为字符串返回。

BOOST_CURRENT_FUNCTION 提供了一种独立于平台的方法来检索函数的名称。从 C++11 开始,您可以使用标准化宏 __func__ 做同样的事情。在 C++11 之前,Visual C++ 和 GCC 等编译器支持宏 __FUNCTION__ 作为扩展。 BOOST_CURRENT_FUNCTION 使用编译器支持的任何宏。

如果使用 Visual C++ 2013 编译,示例 69.2 显示 int __cdecl main(void)。

示例 69.3。使用 boost::prior() 和 boost::next()

#include <boost/next_prior.hpp>
#include <array>
#include <algorithm>
#include <iostream>
int main()
{
  std::array<char, 4> a{{'a', 'c', 'b', 'd'}};
  auto it = std::find(a.begin(), a.end(), 'b');
  auto prior = boost::prior(it, 2);
  auto next = boost::next(it);
  std::cout << *prior << '\n';
  std::cout << *it << '\n';
  std::cout << *next << '\n';
}

Boost.Utility 提供了两个函数,boost::prior() 和 boost::next(),它们返回一个相对于另一个迭代器的迭代器。在示例 69.3 中,它指向数组中的“b”,prior 指向“a”,然后指向“d”。

与 std::advance() 不同,boost::prior() 和 boost::next() 返回一个新的迭代器并且不修改传入的迭代器。

除了迭代器之外,这两个函数都接受第二个参数,该参数指示向前或向后移动的步数。在示例 69.3 中,迭代器在对 boost::prior() 的调用中向后移动了两步,在对 boost::next() 的调用中向前移动了一步。

步数始终为正数,即使对于向后移动的 boost::prior() 也是如此。

要使用 boost::prior() 和 boost::next(),请包含头文件 boost/next_prior.hpp。

这两个函数都被添加到 C++11 的标准库中,它们被称为 std::prev() 和 std::next()。它们在头文件迭代器中定义。

示例 69.4。使用 boost::noncopyable

#include <boost/noncopyable.hpp>
#include <string>
#include <utility>
#include <iostream>
struct animal : boost::noncopyable
{
  std::string name;
  int legs;
  animal(std::string n, int l) : name{std::move(n)}, legs{l} {}
};
void print(const animal &a)
{
  std::cout << a.name << '\n';
  std::cout << a.legs << '\n';
}
int main()
{
  animal a{"cat", 4};
  print(a);
}

Boost.Utility 提供类 boost::noncopyable,它在 boost/noncopyable.hpp 中定义。此类使复制(和移动)对象变得不可能。

通过将复制构造函数和赋值运算符定义为私有成员函数,或者从 C++11 开始,通过使用 delete 删除复制构造函数和赋值运算符,可以达到相同的效果。然而,从 boost::noncopyable 派生明确说明类的对象应该是不可复制的。

注意:

一些开发人员更喜欢 boost::noncopyable,而其他开发人员更喜欢使用 delete 显式删除成员函数。您会在 Stack Overflow 等地方找到这两种方法的论点。

可以编译和执行示例 69.4。但是,如果将 print() 函数的签名修改为按值而不是按引用获取动物类型的对象,则生成的代码将不再编译。

示例 69.5。使用 boost::addressof()

#include <boost/utility/addressof.hpp>
#include <string>
#include <iostream>
struct animal
{
  std::string name;
  int legs;
  int operator&() const { return legs; }
};
int main()
{
  animal a{"cat", 4};
  std::cout << &a << '\n';
  std::cout << boost::addressof(a) << '\n';
}

为了检索特定对象的地址,即使运算符 & 已被重载,Boost.Utility 提供了函数 boost::addressof(),它在 boost/utility/addressof.hpp 中定义(参见示例 69.5)。在 C++11 中,此函数成为标准库的一部分,并在头文件内存中作为 std::addressof() 提供。

示例 69.6。使用 BOOST_BINARY

#include <boost/utility/binary.hpp>
#include <iostream>
int main()
{
  int i = BOOST_BINARY(1001 0001);
  std::cout << i << '\n';
  short s = BOOST_BINARY(1000 0000 0000 0000);
  std::cout << s << '\n';
}

宏 BOOST_BINARY 允许您创建二进制形式的数字。标准 C++ 仅支持十六进制和八进制形式,使用前缀 0x 和 0。C++11 引入了用户定义文字,允许您定义自定义后缀,但在 C+ 中仍然没有以二进制形式使用数字的标准方法+11。

示例 69.6 显示 145 和 -32768。 s 中存储的位序列表示一个负数,因为 16 位类型的 short 使用第 16 位——short 中的最高有效位——作为符号位。

BOOST_BINARY 只是提供了另一种写数字的选项。因为在 C++ 中,数字的默认类型是 int,所以 BOOST_BINARY 也使用 int。要定义一个 long 类型的数字,请使用宏 BOOST_BINARY_L,它生成等同于以字母 L 为后缀的数字。

Boost.Utility 包括额外的宏,例如 BOOST_BINARY_U,它初始化一个没有符号位的变量。所有这些宏都在头文件 boost/utility/binary.hpp 中定义。

示例 69.7。使用 boost::string_ref

#include <boost/utility/string_ref.hpp>
#include <iostream>
boost::string_ref start_at_boost(boost::string_ref s)
{
  auto idx = s.find("Boost");
  return (idx != boost::string_ref::npos) ? s.substr(idx) : "";
}
int main()
{
  boost::string_ref s = "The Boost C++ Libraries";
  std::cout << start_at_boost(s) << '\n';
}

Example 69.7

示例 69.7 引入了类 boost::string_ref,它是对仅支持读取访问的字符串的引用。在某种程度上,该引用可与 const std::string& 相媲美。然而,const std::string& 要求存在一个 std::string 类型的对象。 boost::string_ref 也可以在没有 std::string 的情况下使用。 boost::string_ref 的好处是,与 std::string 不同,它不需要分配内存。

示例 69.7 在字符串中查找单词“Boost”。如果找到,将显示以该词开头的字符串。如果未找到“Boost”一词,则会显示一个空字符串。 main() 中字符串 s 的类型不是 std::string,而是 boost::string_ref。因此,没有内存分配给 new,也没有创建副本。 s 直接指向文字字符串“The Boost C++ Libraries”。

start_at_boost() 的返回值类型是 boost::string_ref,而不是 std::string。该函数不返回新字符串,它返回一个引用。此引用指向参数的子字符串或空字符串。 start_at_boost() 要求原始字符串在使用 boost::string_ref 类型的引用时保持有效。如果可以保证这一点,如示例 69.7 所示,则可以避免内存分配。

还可以使用其他实用程序,但它们超出了本书的范围,因为它们主要由 Boost 库的开发人员使用或用于模板元编程。 Boost.Utility 的文档提供了这些附加实用程序的相当全面的概述,如果您有兴趣,可以作为起点。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们