基本概念这里不再浪费时间介绍了,代码中介绍了断言,不可继承类,加强型bool,uuid生成,异常处理的使用方法
#include <iostream>
#include <string>
//BOOST断言头文件:
#include <boost/assert.hpp>
//BOOST静态断言头文件:
#include <boost/static_assert.hpp>
//BOOST不可继承类:
#include <boost/noncopyable.hpp>
//BOOST加强型bool:
#include <boost/logic/tribool.hpp>
#include <boost/logic/tribool_io.hpp>
//BOOST uuid生成:
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
class MyClass:boost::noncopyable {};
class my_category :public boost::system::error_category
{
public:
virtual const char * name() const BOOST_SYSTEM_NOEXCEPT
{
return "my_category";
}
virtual std::string message(int ev)const
{
std::string msg;
switch (ev)
{
case 0:
{
msg = "ok";
break;
}
default:
msg = "error";
break;
}
return msg;
}
};
int main() {
/*
//断言
BOOST_ASSERT(0x10 == 16); //正常断言
BOOST_ASSERT(0x10 == 16); //触发断言(无提示词)
BOOST_ASSERT_MSG(0x10 == 17, "WdIg"); //Assertion failed: (0x10 == 17)&&("WdIg")
//静态断言(编译器)
BOOST_STATIC_ASSERT(0x10 == 16); //静态断言,编译出现错误:静态断言失败,原因是 "0x10 == 17"
BOOST_STATIC_ASSERT_MSG(0x10 == 17, "WdIg"); //静态断言,静态断言失败,原因是 "WdIg"
MyClass obj1;
MyClass obj2(obj1);
MyClass obj3 = obj1; //E1776 无法引用 函数 "MyClass::MyClass(const MyClass &)" (已隐式声明) --它是已删除的函数
//boost加强型bool加入了一种新的bool值:未知:indeterminate
boost::tribool tb1(true);
boost::tribool tb2(!tb1);
if (tb2)
{
std::cout << "true" << std::endl;
}
tb2 = boost::indeterminate;
if (tb2 == boost::indeterminate)
{
std::cout << "indeterminate" << std::endl;
}
if (boost::indeterminate(tb2))
{
std::cout << "indeterminate" << std::endl;
}
std::cout << (tb2 || true) << std::endl;
std::cout << (tb2 && false) << std::endl;
//uuid生成:
//SHA1摘要:
boost::uuids::uuid szStr = boost::uuids::string_generator()("{0123456789abcedf0123456789abcdef}");
boost::uuids::name_generator ngen(szStr);
boost::uuids::uuid obj = ngen("wdigeer");
std::cout << obj << std::endl;
//随机数生成:
boost::uuids::random_generator rgen;
boost::uuids::uuid obj1 = rgen();
std::cout << obj1 << std::endl;
*/
my_category my_cat;
boost::system::error_code ec(10, my_cat);
std::cout << ec.value() << "\t" << ec.message() << std::endl;
ec = boost::system::error_code(10, boost::system::system_category());
std::cout << ec.value() << "\t" << ec.message() << std::endl;
std::cout << boost::system::system_category().name() << std::endl;
boost::system::error_code ec2;
if (ec2.value() == boost::system::errc::success)
{
std::cout << "success" << std::endl;
}
if (!ec2)
{
std::cout << "success" << std::endl;
}
ec2.assign(5, boost::system::system_category());
std::cout << ec2.value() << "\t" << ec2.message() << std::endl;
try
{
throw boost::system::system_error(boost::system::error_code(5, boost::system::system_category()));
}
catch (boost::system::system_error& e)
{
std::cout << e.what() << std::endl;
}
system("pause");
return 0;
}