小编Asp*_*nca的帖子

当与顺序执行策略一起使用时,std :: any_of是否保证迭代顺序?

我有一个过滤器功能列表。如果这些函数中的任何一个返回“ true”,则不应进一步处理事件。

std :: any_of似乎适合此用例,但我想保证按其添加到我的列表的顺序调用过滤器函数(因为它们可能会产生副作用)。因此,如果我使用std :: any_of,我需要知道它调用过滤器函数的顺序是确定性的,从列表的begin()到end()。

我已经检查了std :: any_of上的C ++标准和顺序执行策略,但是没有提到顺序保证。我没有找到关于cppreference的顺序保证的提法,也没有在stackoverflow上找到足够类似的答案。

我的结论是,没有订单保证。尽管大多数编译器可能会按顺序处理这些函数,但我不应该依赖它。

bool MyClass::event(QEvent* event)
{
    std::vector<std::function<bool(QEvent*)> > functionList = getCurrentEventFilters();

    const auto bFilter = std::any_of(functionList .begin(),
                                     functionList .end(),
                                     [&event](std::function<bool(QEvent*)> function) {return function(event);});
    if (bFilter)
    {
        return true;
    }

    return Base::event(event);
}
Run Code Online (Sandbox Code Playgroud)

c++ std

7
推荐指数
2
解决办法
135
查看次数

如何避免C++中operator ==实现中的错误?

我经常有一些类提供简单的逐个成员比较:

class ApplicationSettings
{
public:
   bool operator==(const ApplicationSettings& other) const;
   bool operator!=(const ApplicationSettings& other) const;

private:
   SkinType m_ApplicationSkin;
   UpdateCheckInterval m_IntervalForUpdateChecks;
   bool m_bDockSelectionWidget;
   // Add future members to operator==
};

bool ApplicationSettings::operator==(const ApplicationSettings& other) const
{
   if (m_ApplicationSkin != other.m_ApplicationSkin)
   {
      return false;
   }

   if (m_IntervalForUpdateChecks != other.m_IntervalForUpdateChecks)
   {
      return false;
   }

   if (m_bDockSelectionWidget != other.m_bDockSelectionWidget)
   {
      return false;
   }

   return true;
}

bool ApplicationSettings::operator!=(const ApplicationSettings& other) const;
{
   return ( ! operator==(other));
}
Run Code Online (Sandbox Code Playgroud)

鉴于此时C++没有提供任何构造来生成运算符==,是否有更好的方法来确保未来成员成为比较的一部分,而不是我在数据成员下面添加的注释?

c++ operators

4
推荐指数
1
解决办法
268
查看次数

标签 统计

c++ ×2

operators ×1

std ×1