【问题标题】:QtConcurrent::run() can't handle more than 5 arguments?QtConcurrent::run() 不能处理超过 5 个参数?
【发布时间】:2013-06-04 12:02:36
【问题描述】:

将具有 6 个或更多参数的函数传递给 QtConcurrent::run() 时出现编译错误。当我将它们减少到 5 个参数时,我不再收到此错误。

这个虚拟代码为我重现了错误:

void foo(int, int, int, int, int, int)
{

}

QtConcurrent::run(foo, 1, 2, 3, 4, 5, 6);

编译错误是:

error: no matching function for call to 'run(void (&)(int, int, int, int, int, int), int, int, int, int, int, int)'

应该是这样吗? QtConcurrent::run() 真的最多只能有 5 个参数吗?

【问题讨论】:

  • 如果它是有限的,我不会感到惊讶。毕竟,它必须与 C++11 之前的编译器一起工作。

标签: c++ qt compiler-errors qtconcurrent


【解决方案1】:

参见qtconcurrentrun.h

template <typename T, typename Param1, typename Arg1, typename Param2, typename Arg2, typename Param3, typename Arg3, typename Param4, typename Arg4, typename Param5, typename Arg5>
QFuture<T> run(T (*functionPointer)(Param1, Param2, Param3, Param4, Param5), const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4, const Arg5 &arg5);

函数可以接受 5 个参数

【讨论】:

    【解决方案2】:

    一方面,您可以使用 std::bindboost::bind 传递超过 5 个参数(在 C++11 的情况下无限制):

    QFuture<void> result = QtConcurrent::run(std::bind(&foo, 1, 2, 3, 4, 5, 6));
    

    另一方面,对于每个函数,5 个参数应该就足够了。您可能需要重新考虑您的设计以减少函数参数的数量。您可以将一些对象传递给函数。

    Data d;
    QFuture<void> result = QtConcurrent::run(foo, d);
    

    另外不要忘记 Concurrent::run 在 5.3 之前的版本中有时可以hung 无缘无故。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多