对于 errorCode.category().name() 输出“asio.misc”和 errorCode.message() 输出“文件结束”的错误,以下比较失败
如果它声称属于 asio.misc 类别,那么为什么 (errorCode.category() == boost::asio::error::misc_category ) 的 if 条件评估为 false?
谷歌搜索(包括此处的答案)说 boost::system::error_code 在多个类别中可以具有相同的值,因此我假设为了获得正确的消息和含义,我们必须比较 boost::system::error_category以及 boost::system::error_code::value。
如果这不起作用,我们如何正确比较类别?
有问题的代码:
//--------------------------------------------------------------------------------------------------
std::string ClientSocketASIO::ErrorCodeToString(const boost::system::error_code & errorCode)
{
std::ostringstream debugMsg;
debugMsg << " Error Category: " << errorCode.category().name() << ". "
<< " Error Message: " << errorCode.message() << ". ";
if( errorCode.category() == boost::asio::error::misc_category )
{
switch (errorCode.value())
{
case boost::asio::error::eof:
debugMsg << ". Server has disconnected.";
break;
case boost::asio::error::connection_refused:
debugMsg << ". Connection Refused";
break;
default:
debugMsg << ". Unknown Error.";
break;
}
}
else
{
debugMsg << ". Unknown Error category.";
}
return debugMsg.str();
}
EDIT:
根据https://theboostcpplibraries.com/boost.system https://theboostcpplibraries.com/boost.system and Boost::file_system:检查错误代码 https://stackoverflow.com/questions/23848942/boostfile-system-checking-error-codes/23849966#23849966
人们通常错误地编写代码,只比较错误值而不比较类别。错误值本身并不唯一,并且可能出现在多个类别中。用户还可以自由创建自己的错误代码和类别。因此,必须将两者进行比较。我认为,这不会影响大量应用程序,因为无论如何它们都只使用一种功能或库来提升其项目和/或大多数错误代码都映射到 Windows 错误代码,这会尽最大努力不这样做碰撞。
我必须在另一台计算机上才能访问增强邮件列表,因为我的工作几乎阻止了所有内容。
http://boost.2283326.n4.nabble.com/Compare-boost-system-error-category-td4692861.html#a4692869 http://boost.2283326.n4.nabble.com/Compare-boost-system-error-category-td4692861.html#a4692869
据那边的人说,比较失败的原因是boost库是静态链接的,boost::system::error_category::operator ==比较地址,所以左手是一个lib中的地址,右手是一个lib中的地址是另一个中的地址。当他们期望的时候,他们不会是平等的。
对我来说,使用运算符 == 的地址似乎是一个非常愚蠢的举动。如果发现任何新知识,我将继续在 boost 邮件列表上对此进行咆哮,并在这里为其他人进行编辑。
现在,我动态链接并使用表单
//--------------------------------------------------------------------------------------------------
std::string ClientSocketASIO::ErrorCodeToString(const boost::system::error_code & errorCode)
{
std::ostringstream debugMsg;
debugMsg << " Error Category: " << errorCode.category().name() << ". "
<< " Error Message: " << errorCode.message() << ". ";
// IMPORTANT - These comparisons only work if you dynamically link boost libraries
// Because boost chose to implement boost::system::error_category::operator == by comparing addresses
// The addresses are different in one library and the other when statically linking.
//
// We use make_error_code macro to make the correct category as well as error code value.
// Error code value is not unique and can be duplicated in more than one category.
if (errorCode == make_error_code(boost::asio::error::connection_refused))
{
debugMsg << ". Connection Refused";
}
else if (errorCode == make_error_code(boost::asio::error::eof))
{
debugMsg << ". Server has disconnected.";
}
else
{
debugMsg << ". boost::system::error_code has not been mapped to a meaningful message.";
}
return debugMsg.str();
}
但是,当我静态链接以提升时,这不起作用。如果有人对我们如何正确比较 boost::system::error_code 并获得预期结果有更多建议,请让我们深入探讨。