是否有可能以一个boost ::系统::错误_ code转换为一个std:错误_ code? [英] Is it possible to convert a boost::system::error_code to a std:error_code?

查看:407
本文介绍了是否有可能以一个boost ::系统::错误_ code转换为一个std:错误_ code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想如果存在的话,以取代外部库(如升压)尽可能地与他们在标准C等值++,这是可能的,以减少依赖,所以我不知道是否存在一个安全的方式来转换的boost ::系统::错误_ code 的std ::错误_ code 。伪code例如:

I want to replace external libraries (like boost) as much as possible with their equivalents in standard C++ if they exist and it is possible, to minimize dependencies, therefore I wonder if there exists a safe way to convert boost::system::error_code to std::error_code. Pseudo code example:

void func(const std::error_code & err)
{
    if(err) {
        //error
    } else {
        //success
    }
}

boost::system::error_code boost_err = foo(); //foo() returns a boost::system::error_code
std::error_code std_err = magic_code_here; //convert boost_err to std::error_code here
func(std_err);

最重要的它是不完全一样的误差,只是这样接近越好,在最后如果是出错或没有。是否有任何巧妙的解决方案?

The most important it is not the exactly the same error, just so close to as possible and at last if is an error or not. Are there any smart solutions?

在此先感谢!

推荐答案

由于C ++ - 11(的std :: ERRC),<一个href=\"http://www.boost.org/doc/libs/1_39_0/boost/system/error_$c$c.hpp\">boost/system/error_$c$c.hpp映射了同样的错误codeS到的std :: ERRC ,这是在系统中头文件中定义 SYSTEM_ERROR

Since C++-11 (std::errc), boost/system/error_code.hpp maps the same error codes to std::errc, which is defined in the system header system_error.

可以比较这两个枚举和,因为它们都似乎是基于POSIX标准他们应该是功能上等同的。可能需要转换。

You can compare both enums and they should be functionally equivalent because they both appear to be based on the POSIX standard. May require a cast.

例如,

namespace posix_error
    {
      enum posix_errno
      {
        success = 0,
        address_family_not_supported = EAFNOSUPPORT,
        address_in_use = EADDRINUSE,
        address_not_available = EADDRNOTAVAIL,
        already_connected = EISCONN,
        argument_list_too_long = E2BIG,
        argument_out_of_domain = EDOM,
        bad_address = EFAULT,
        bad_file_descriptor = EBADF,
        bad_message = EBADMSG,
        ....
       }
     }

的std :: ERRC

address_family_not_supported  error condition corresponding to POSIX code EAFNOSUPPORT  

address_in_use  error condition corresponding to POSIX code EADDRINUSE  

address_not_available  error condition corresponding to POSIX code EADDRNOTAVAIL  

already_connected  error condition corresponding to POSIX code EISCONN  

argument_list_too_long  error condition corresponding to POSIX code E2BIG  

argument_out_of_domain  error condition corresponding to POSIX code EDOM  

bad_address  error condition corresponding to POSIX code EFAULT 

这篇关于是否有可能以一个boost ::系统::错误_ code转换为一个std:错误_ code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆