Is it possible to convert a boost::system::error_code to a std:error_code?(是否可以将 boost::system::error_code 转换为 std:error_code?)
问题描述
我想尽可能地用标准 C++ 中的等价物替换外部库(如 boost),如果它们存在并且有可能,以尽量减少依赖性,因此我想知道是否存在转换 boost 的安全方法::system::error_code
到 std::error_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) 起,boost/system/error_code.hpp 将相同的错误代码映射到 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::system::error_code 转换为 std:error_code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以将 boost::system::error_code 转换为 std:error_code?


- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- C++ 协变模板 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 从python回调到c++的选项 2022-11-16
- Stroustrup 的 Simple_window.h 2022-01-01