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

  1. include <boost/system/error_code.hpp>
  2. #include <iostream>
  3.  
  4. using namespace boost::system;
  5.  
  6. void fail(error_code &ec)
  7. {
  8. ec = errc::make_error_code(errc::not_supported);
  9. }
  10.  
  11. int main()
  12. {
  13. error_code ec;
  14. fail(ec);
  15. std::cout << ec.value() << std::endl;
    std::cout << ec.category() << std::endl;
  16. return ;
  17. }

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

  1. #include <boost/system/error_code.hpp>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. class application_category :
  6. public boost::system::error_category
  7. {
  8. public:
  9. const char *name() const noexcept { return "my app"; }
  10. std::string message(int ev) const { return "error message"; }
  11. };
  12.  
  13. application_category cat;
  14.  
  15. int main()
  16. {
  17. boost::system::error_code ec(, cat);
  18. std::cout << ec.value() << std::endl;
  19. std::cout << ec.category().name() << std::endl;
  20. return ;
  21. }

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

  1. #include <boost/system/error_code.hpp>
  2. #include <iostream>
  3.  
  4. using namespace boost::system;
  5.  
  6. void fail(error_code &ec)
  7. {
  8. ec = errc::make_error_code(errc::not_supported);
  9. }
  10.  
  11. int main()
  12. {
  13. error_code ec;
  14. fail(ec);
  15. boost::system::error_condition ecnd = ec.default_error_condition();
  16. std::cout << ecnd.value() << std::endl;
  17. std::cout << ecnd.category().name() << std::endl;
  18. return ;
  19. }

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. day29—JavaScript中DOM的基础知识应用

    转行学开发,代码100天——2018-04-14 JavaScript中DOM操作基础知识即对DOM元素进行增删改操作.主要表现与HTML元素的操作,以及对CSS样式的操作.其主要应用知识如下图: 通 ...

  2. 电脑可以识别sd卡手机无法识别 的解决方法。 我成功了。 淘宝买的sd卡 不用退货了。 退的人肝疼

    https://wenku.baidu.com/view/822e471055270722192ef736.html 电脑可以识别 sd 卡手机无法识别 * (本教程只是本人实际操作方法,可以解决一部 ...

  3. RichEdit 学习

    procedure TForm1.AddText(RichEdit: TRichEdit; Str: string; TextColor: TColor = clBlack; FontName: st ...

  4. Vagrant 手册之 Provisioning - 概述

    原文地址 通过 Vagrant 中的 provisioner 配置程序,可以在使用 vagrant up 启动虚拟机时,在虚拟机上执行安装软件.更改配置等操作. box 通常是通用的,而每个项目总有自 ...

  5. Selenium+Java环境搭建

    1. 安装JDK URL:http://www.oracle.com/technetwork/java/javase/downloads/ 2. 配置环境变量 JAVA_HOME = E:\Java\ ...

  6. 【Jmeter】利用Jmeter+ant+Jenkins 搭建 接口&性能测试 持续集成平台

    https://www.jianshu.com/p/6ab73a95d53e https://yq.aliyun.com/articles/664329

  7. Spring事务传播及数据库事务操作

    从Spring 事务配置说起 先看看Spring 事务的基础配置 <aop:aspectj-autoproxy proxy-target-class="true"/> ...

  8. Codeforces 1000E We Need More Bosses (边双连通+最长链)

    <题目链接> 题目大意:给定一个$n$个节点$m$条边的无向图,问你对任意两点,最多有多少条特殊边,特殊边指删除这条边后,这两个点不能够到达. 解题分析: 特殊变其实就是指割边,题意就是问 ...

  9. React(6) --双向数据绑定及列表数据循环

    React双向数据绑定:model改变影响view,view改变反过来影响model import React,{Component} from 'react'; class Todolist ext ...

  10. 一、Iframe

    一.Iframe 自适应iframe的高 <!-- frameborder :设置iframe的边框 scrolling:设置iframe的滚动条 src:设置iframe的路径 onload: ...