【问题标题】:QtConcurrency::mapped with a member functionQtConcurrency::映射有成员函数
【发布时间】:2014-07-31 08:56:18
【问题描述】:

如何将QtConcurrent::mapped 与成员函数一起用作运算符?目前我正在使用一个可调用对象(这是一个丑陋的解决方案):

struct Vectorizer {
    Vectorizer(DialogCreateSamples* cs)
    : m_createSamples(cs) { }

    typedef QString result_type;

    QString operator()(const QString &input)
    {
        return m_createSamples->generateVector(input);
    }

    DialogCreateSamples* m_createSamples;
};
QFuture<QString> future = QtConcurrent::mapped(m_inputs,Vectorizer(this));

还尝试传递 lambda 表达式,但编译器说 lambda 中没有定义 result_type。这适用于QtConcurrent::map,因为map 不需要result_type。因此,如果我可以在 lambda 中添加 typedef 它应该可以工作...

【问题讨论】:

    标签: c++ qt c++11 lambda qtconcurrent


    【解决方案1】:

    也许绑定?如果您使用 C++11,则为 std::bind ;否则为 std::tr1::bind 或 boost::bind。

    类似:

    QtConcurrent::mapped(m_inputs, std::bind(&Class::member_function, pointerToObjectOfTypeClass /* this? */, _1 /* placeholder for argument of your function filled by data from m_inputs */));
    

    我们在代码中使用它;例如:https://github.com/clementine-player/Clementine/blob/d03c1aa2419a0ceefd7f65114c1ac8991790b716/src/playlist/playlistbackend.cpp#L187

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2023-03-05
      • 2016-08-08
      • 1970-01-01
      • 2012-04-06
      • 2019-12-12
      • 2021-07-06
      相关资源
      最近更新 更多