boost实用工具

一些有用的boost组件

1. noncopyable

允许程序实现一个禁用拷贝的类

class noncopyable
{
protected:
    noncopyable() {}
    ~noncopyable() {}
private:  // emphasize the following members are private
    noncopyable( const noncopyable& );
    noncopyable& operator=( const noncopyable& );
};

用法:

#include <boost/core/noncopyable.hpp>

class X: private boost::noncopyable
{
};

2.ignore_unused

编写代码过程中有时会出现一些暂时用不到但又必须保留的变量,编译器对此会发出警告,我们可以显式地忽略这些变量,从而消除这些警告信息。

#include<boost/core/ignore_unused.hpp>
int func(int x, int y)
{
    int i;
    ignore_unused(x,i);
    return y;
}

3. optional

在实际的软件开发过程中我们经常遇到无效值的情况,如函数不能总是返回有效值。抛出异常的方法代价高,增加哨兵角色的方法不够通用。

optional使用“容器”语义,包装了“可能产生无效值”的对象,实现了“未初始化”的概念,为这种“无效值”的情形提供了一个更好的解决方案。

#include <boost/optional.hpp>

boost::optional<int> getConfigParam(std::string name);  // return either an int or a `not-an-int`

int main()
{
  if (boost::optional<int> oi = getConfigParam("MaxValue")) // did I get a real int?
    runWithMax(*oi);                                        // use my int
  else
    runWithNoMax();
}

4. assign

重载赋值操作符“+=”,逗号操作符“,”或者括号操作符“()”用简洁的语法方便地对标准容器赋值或者初始化。

5. tribool

 类似于C++中的bool类型,但是基于三态的布尔逻辑:true(真),false(假)和indeterminate(未知)。

#include<boost/logic/tribool.hpp>
#include<boost/logic/tribool_io.hpp>

int main()
{
	tribool tb(true);
	if (tb)  cout << "true" << endl;

	tb = indeterminate;
	cout << tb << endl;
	cout << (tb || true) << endl;
	cout << (tb && false) << endl;
	
	if (tb)   cout << "never reach here" << endl;  //永远不会成立
	if (!tb)  cout << "never reach here" << endl;  //永远不会成立
	if (tb == indeterminate)                       //不会成立
		cout << "indeterminate" << endl;
	if (indeterminate(tb))                         //必须使用indeterminate
		cout << "indeterminate" << endl;
}

out: true,2,1,0,indeterminate

6. operators

完全实现操作符的重载工作是单调乏味的,而且增加了代码量,也增加了出错的可能性,还必须保证这些操作符都实现了正确的语义。

operators允许用户在自己的类里仅定义少量操作符,就可方便地自动生成其他操作符重载,而且保证正确的与i实现。

#include<boost/operators.hpp>

template<typename T>
class MyPoint : boost::totally_ordered<MyPoint<T>>,
	            boost::additive<MyPoint<T>>
{
	T x, y, z;
public:
	explicit MyPoint(int a = 0, int b = 0, int c = 0) :x(a), y(b), z(c) {}
	void print() const
	{	cout << x << "," << y << "," << z << endl;	}

public:
	friend bool operator<(const MyPoint& l, const MyPoint& r)
	{
		return (l.x*l.x + l.y*l.y + l.z*l.z) < (r.x*r.x + r.y*r.y + r.z*r.z);
	}
	friend bool operator==(const MyPoint& l, const MyPoint& r)
	{	return l.x == r.x && l.y == r.y && l.z == r.z;	}
	MyPoint& operator+=(const MyPoint& r)
	{
		x += r.x;
		y += r.y;
		z += r.z;
		return *this;
	}
	MyPoint& operator-=(const MyPoint& r)
	{
		x -= r.x;
		y -= r.y;
		z -= r.z;
		return *this;
	}
};

int main()
{
	MyPoint<double> p0, p1(1, 2, 3), p2(3, 0, 5), p3(3, 2, 1);
	cout << (p0 < p1) << endl;
	cout << (p2 >= p3) << endl;
	cout << (p1 != p2) << endl;
	p3 += p2;
	p3.print();
	MyPoint<double> p4 = p1 + p2;
	p4.print();
}

7.exception

boost.exception库针对标准库中异常类的缺陷进行了强化,提供<<操作符重载,可以向异常传入任意数据,有助于增加异常的信息和表达力。

#include<boost/exception/all.hpp>
//自定义异常类
struct my_exception :virtual std::exception, virtual boost::exception
{};
//异常信息的类型
typedef boost::error_info<struct tag_err_no, int> err_no;
typedef boost::error_info<struct tag_err_str, string> err_str;

int main()
{
	try {
		try
		{
			throw my_exception() << err_no(10);
		}
		catch (my_exception& e)
		{
			cout << *get_error_info<err_no>(e) << endl;  //获得异常内存储的信息
			cout << e.what() << endl;
			e << err_str("other info");    //向异常追加信息
			throw;                         //再次抛出异常
		}
	}
	catch (my_exception& e)
	{
		cout << *get_error_info<err_str>(e) << endl;
	}
}

8.uuid

UUID: "Universally Unique Identifier"。不需要有一个中央认证机构就可以创建全球唯一标识符。没有构造函数,需要指定生成器。

#include<boost/uuid/uuid.hpp>
#include<boost/uuid/uuid_generators.hpp>
#include<boost/uuid/uuid_io.hpp>
using namespace boost::uuids;

9.config

主要提供给Boost库开发者,将程序的编译配置分解为三个部分:平台、编译器和标准库,帮助他们解决特定平台和编译器的兼容问题。

BOOST_STRINGIZE可以将任意字面量转换为字符串,只能用于编译器,不支持运行时转换。最常见的用途实在泛型编程中将编译器常数转换为字符串,在调试或者输出日志时相当方便。

BOOST_STATIC_CONSTANT 静态常量类型, 无需担心编译器的能力限制,代码具可移植性,类似普通变量赋值:

BOOST_STATIC_CONSTANT(int, v1 = 10);相当于 static const int v1 = 10;

 

10. utility

包含了若干很小但有用的工具。

BOOST_BINARY

用于实现简单的二进制常量显示,将一组或多组01数字在编译期间展开成一个八进制数字。每个数字之间需要空格分隔,每组可以容纳一个或八个0/1数字。还有BOOST_BINARY_UL,BOOST_BINARY_LL。

BOOST_CURRENT_FUNCTION

获得包含该宏的外围函数的名称,一个包含完整函数声明的编译器字符串。在配合抛出异常或者输出诊断日志时非常有用。

#include<boost/current_function.hpp>

double func()
{
	cout << BOOST_CURRENT_FUNCTION << endl;
	return 0.0;
}

//string str = BOOST_CURRENT_FUNCTION;    //error, 在函数作用域外无意义

int main()
{
	int x = BOOST_BINARY(0110);
	cout << x << endl;

	//cout << str << endl;
	cout << __FUNCTION__ << endl;
	cout << BOOST_CURRENT_FUNCTION << endl;
	func();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值