比较boost :: system :: error_category [英] Compare boost::system::error_category

查看:132
本文介绍了比较boost :: system :: error_category的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下比较因错误而失败,该错误输出errorCode.category().name()的"asio.misc"和errorCode.message()的文件结尾"

The following comparison fails for an error whom outputs "asio.misc" for errorCode.category().name() and "end of file" for errorCode.message()

如果声称属于asio.misc类别,那么为什么(errorCode.category()== boost :: asio :: error :: misc_category)的if条件评估为假?

If it claims to be in category asio.misc, then why does the if condition of (errorCode.category() == boost::asio::error::misc_category ) evaluate to false?

在Google搜索(包括此处的答案)中说,boost :: system :: error_code在多个类别中可以具有相同的值,因此我认为为了获得正确的消息和含义,我们必须比较boost :: system :: error_category以及boost :: system :: error_code :: value.

Googling (including answers here) says that a boost::system::error_code can have the same value in more than one category, so I assume that in order to get the proper message and meaning we must compare boost::system::error_category as well as boost::system::error_code::value.

如果此方法不起作用,我们如何正确比较类别?

How do we properly compare the category if this fails to work?

有问题的代码:

//--------------------------------------------------------------------------------------------------
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();
}



根据 https://theboostcpplibraries.com/boost.system Boost::file_system:检查错误代码

人们通常会错误地编写代码,仅比较错误值而不是类别.单独的错误值不是唯一的,并且可能出现在多个类别中.用户还可以自由创建自己的错误代码和类别.因此,必须将两者进行比较.我认为这不会影响大量的应用程序,因为它们无论如何都只使用一种功能或增强项目的库,并且/或者大多数错误代码都映射到Windows错误代码,而这些错误代码已尽最大努力避免了.碰撞.

People commonly and mistakenly write code only comparing the error value and not the category. The error value alone is not unique and may occur in more than one category. Users are also free to create their own error codes and categories. Therefore, both must be compared. This, I assume, doesn't effect a large number of applications, because they are only using one feature or library of boost their project anyway and/or the majority of error codes are mapped to windows error codes which made a best effort to not collide.

由于我的工作几乎阻止了所有工作,因此我必须上另一台计算机才能查看增强邮件列表.

I had to get on another computer to hit up the boost mailing list, since my work blocks almost everything.

http://boost .2283326.n4.nabble.com/Compare-boost-system-error-category-td4692861.html#a4692869

根据那里的一个小伙子,比较失败的原因是因为boost库是静态链接的,并且boost :: system :: error_category :: operator ==比较地址,所以LHS是一个库中的地址RHS是另一个地址.当期望达到预期时,它们将不会相等.

According to a fella over there, the reason the comparison is failing is because the boost library is statically linked, and boost::system::error_category::operator == compares addresses, so the LHS is the address in one lib and the RHS is the address in another. They are not going to be equal when they are expected to be.

使用运算符==的地址对我来说似乎是很愚蠢的举动.如果发现任何新知识,我将继续在增强邮件列表中夸奖它,并在此处为其他人进行编辑.

Using the address for operator == seems like a very silly move to me. I will continue to rant about it on the boost mailing list and edit here for others if any new knowledge is discovered.

目前,我正在动态链接并使用表格

For now, I am dynamically link and use form

//--------------------------------------------------------------------------------------------------
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并获得预期结果有更多建议,请让我们深入了解.

however, this does not work when I link statically to boost either. If anyone has any more suggestions on how we are to properly compare boost::system::error_code and get expected results, please let's get to the bottom of this.

推荐答案

C ++ 11标准暗示每个错误类别实例应具有 全球唯一的地址和平等的比较应使用 地址进行比较.不幸的是,这在便携式设备中并不可靠 代码,只有Dinkumware STL实现真正的地址唯一性 在过程中的任何地方,它增加了一个完整的内存屏障来实现 那就是很贵.

The C++ 11 standard implies that each error category instance shall have a globally unique address and comparisons of equality shall use that address to compare. Unfortunately, this is not reliable in portable code, only the Dinkumware STL implements true address uniqueness anywhere in the process and it adds a full memory barrier to achieve that i.e. it's expensive.

Boost的实现质量与libstdc ++或libc ++相同, 在某些情况下,您可以获得多个实例化 可以有不同的地址.

Boost's quality of implementation is the same as libstdc++ or libc++, you can get multiple instantiations in certain circumstances and those can have differing addresses.

在我自己的代码中,我首先执行比较运算符,如果失败,我 对类别的name()进行strcmp().到目前为止,这还没有咬我. 我个人认为错误类别的这方面是 指定的标准中的缺陷,它强制使用非标头 实施,如​​果您希望它符合标准,即使这样也不符合 掩盖RTLD_LOCAL,这意味着您需要使用已命名的共享 记忆或一些骇客.

In my own code, I do the comparison operator first, and if that fails I do a strcmp() of the category's name(). To date, this has not bitten me. I would personally consider this aspect of error categories to be a defect in the standard, as specified it forces non-header-only implementation if you want it to be conforming, and even that doesn't cover RTLD_LOCAL which means you need to fall back onto named shared memory or some hack.

这篇关于比较boost :: system :: error_category的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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