1

如何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 表达式,但编译器说result_typelambda 中没有定义。这适用于QtConcurrent::map因为map不需要result_type. 所以如果我可以typedef在 lambda 中添加一个它应该可以工作......

4

1 回答 1

3

也许绑定?如果您使用 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

希望能帮助到你。

于 2014-07-31T10:00:11.990 回答