boost::system::error_code is the most basic class in Boost.System; it represents operating system-specific errors. Because operating systems typically enumerate errors, boost::system::error_code saves an error code in a variable of type int.

1. error_code

include <boost/system/error_code.hpp>
#include <iostream> using namespace boost::system; void fail(error_code &ec)
{
ec = errc::make_error_code(errc::not_supported);
} int main()
{
error_code ec;
fail(ec);
std::cout << ec.value() << std::endl;
std::cout << ec.category() << std::endl;
return ;
}

boost::system::errc::not_supported is a number and ec is an object of type boost::system::error_code, the function boost::system::errc::make_error_code() is called. This function creates an object of type boost::system::error_code with the respective error code.

value() member function returns the error code stored in the object.

Error codes of type boost::system::error_code belong to a category that can be retrieved with the member function category(). Errors created with boost::system::errc::make_error_code() automatically belong to the generic category. This is the category errors belong to if they aren’t assigned to another category explicitly.

2. creating error category

#include <boost/system/error_code.hpp>
#include <string>
#include <iostream> class application_category :
public boost::system::error_category
{
public:
const char *name() const noexcept { return "my app"; }
std::string message(int ev) const { return "error message"; }
}; application_category cat; int main()
{
boost::system::error_code ec(, cat);
std::cout << ec.value() << std::endl;
std::cout << ec.category().name() << std::endl;
return ;
}

A new error category is defined by creating a class derived from boost::system::error_category. This requires you to define various member functions. At a minimum, the member functions name() and message() must be supplied because they are defined as pure virtual member functions in boost::system::error_category. While name() returns the name of the error category, message() is used to retrieve the error description for a particular error code.

3. error_condition

#include <boost/system/error_code.hpp>
#include <iostream> using namespace boost::system; void fail(error_code &ec)
{
ec = errc::make_error_code(errc::not_supported);
} int main()
{
error_code ec;
fail(ec);
boost::system::error_condition ecnd = ec.default_error_condition();
std::cout << ecnd.value() << std::endl;
std::cout << ecnd.category().name() << std::endl;
return ;
}

boost::system::error_condition is used just like boost::system::error_code.

While the class boost::system::error_code is used for platform-dependent error codes, boost::system::error_condition is used to access platform-independent error codes. The member function default_error_condition() translates a platform-dependent error code into a platform-independent error code of type boost::system::error_condition.

boost system的更多相关文章

  1. boost中g++ 链接undefined reference to `boost::system::generic_category()问题

    编译错误如下: g++ -std=c++11  tcp_session.cpp tcp_server.cpp test.cpp -o test -pthread/tmp/ccv4rZkD.o: In ...

  2. c++ boost asio库初学习

    前些日子研究了一个c++的一个socket库,留下范例代码给以后自己参考. 同步server: // asio_server.cpp : コンソール アプリケーションのエントリ ポイントを定義します. ...

  3. 如何在多线程leader-follower模式下正确的使用boost::asio。

    #include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream& ...

  4. BOOST.Asio——Tutorial

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

  5. BOOST.Asio——Overview

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

  6. boost asio sync

    Service: #include<boost/asio.hpp> #include<boost/thread.hpp> #include<iostream> #i ...

  7. 网络库crash以及boost asio strand dispath分析

    最近在做服务器的稳定性的相关测试,服务器的网络底层使用的是boost asio,然后自己做的二次封装以更好的满足需求. 服务器昨天晚上发现crash了一次,之前测试了将近半个多月,有一次是莫名的退出了 ...

  8. boost asio tcp server 拆分

    从官方给出的示例中对于 boost::asio::ip::tcp::acceptor 类的使用,是直接使用构造函数进行构造对象,这一种方法用来学习是一个不错的方式. 但是要用它来做项目却是不能够满足我 ...

  9. ros与下位机通信常用的c++ boost串口应用

    一.首先移植c++ boost 库: 1. 先去 Boost官网 下载最新的Boost版本, 我下载的是boost_1_6_0版本, 解压. 2. 进入解压后目录: cd boost_1_6_0, 执 ...

随机推荐

  1. linux ( CentOS 7)下Tengine(nginx)的安装与配置

    TengineTengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.它的目的是打造一个高效.安全的Web平台. 使用root用户安装 ...

  2. 131、TensorFlow保存模型

    # tf.train.Saver类提供了保存和恢复模型的方法 # tf.train.Saver的构造函数 提供了save和恢复的参数选项 # Saver对象提供了方法来运行这些计算节点,制定了写和读的 ...

  3. day13—CSS之导航栏

    转行学开发,代码100天——2018-03-29 见过这么多网页,没有导航栏的几乎没见过.因此,对导航栏的练习非常之必要. 导航栏的实现,基本上都是通过链接列表实现,即ul->li->a标 ...

  4. Jenkins+Gitlab+自动化测试配置持续集成

    Jenkins安装在win7上 GitLab安装在docker上 需求:本地提交自动化测试代码在gitlab上后,jenkins自动构建,拉下新提交的自动化代码,并且运行 参考的链接: https:/ ...

  5. MAC-安装套件管理工具Homebrew

    前言 Homebrew是一款Mac OS下的套件管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能. Homebrew安装 1,Homebrew官网获取安装指令,官网地址:https://br ...

  6. is_numeric漏洞总结

    将16进制数据判断为数据,这样在存入数据库后,数据库会把16进制解析成字符串,可能造成二次注入 转载: https://www.jianshu.com/p/e7cf997d6ccb

  7. STP基本概念及实验

    相关命令(华为交换机): stp enable(在交换机开启stp:stp使能) display stp 查看stp状态 stp mode stp/rstp/mstp  启用stp/rstp/mstp ...

  8. 监控服务器的脚本log_agent

    监控服务器脚本: 将恶意攻击IP地址加入黑名单 1.分割日志 使用os.system 执行操作系统命令,使用重定向来分割日志 2.获取访问ip 读日志文件,获取访问ip记录,使用字符串.split来获 ...

  9. Spring cloud学习--Zuul02

    过滤器 Zuul包括两部分内容:请求的路由和过滤.而实际上请求的路由也是通过过滤器实现的,例如理由映射主要通过pre类型的过滤器完成,它将请求路径与配置的路由规则进行匹配,找到需要转发的目标地址:请求 ...

  10. Java中的多线程基础

    1.线程与进程 进程: 进程是程序运行以及资源分配的基本单位,一个程序至少有一个进程. 如下图所示: 线程: 线程是CPU调度和分配的基本单位,一个进程至少有一个线程. 同一个进程中的线程共享进程资源 ...