Results 1 to 6 of 6

Thread: Using lambdas with QtConcurrent::blockingMappedReduced does not compile

  1. #1
    Join Date
    Nov 2012
    Posts
    21
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Using lambdas with QtConcurrent::blockingMappedReduced does not compile

    Hello, I've been trying to get QtConcurrent::blockingMappedReduced to run using a couple of lambda's and keep running into a compile issue which I can't get to the bottom of. I've cut it right back and am currently using Qt's own example code converted into lambda's and getting the same error. So here it is:

    Qt Code:
    1. typedef QMap<QString, int> WordCount;
    2.  
    3. void ClassName::ProcessStuff() {
    4. auto wordMapFn = [&](const QString &file) -> WordCount {
    5. QFile f(file);
    6. f.open(QIODevice::ReadOnly);
    7. QTextStream textStream(&f);
    8. WordCount wordCount;
    9.  
    10. while (textStream.atEnd() == false)
    11. foreach (QString word, textStream.readLine().split(" "))
    12. wordCount[word] += 1;
    13.  
    14. return wordCount;
    15. };
    16.  
    17. auto wordReduceFn = [&](WordCount &result, const WordCount &w) {
    18. QMapIterator<QString, int> i(w);
    19. while (i.hasNext()) {
    20. i.next();
    21. result[i.key()] += i.value();
    22. }
    23. };
    24. QStringList files;
    25. WordCount total = QtConcurrent::blockingMappedReduced(files.begin(), files.end(), wordMapFn, wordReduceFn);
    26. }
    To copy to clipboard, switch view to plain text mode 

    Compiling this in VS2012 with Qt5.0.2 produces the following error:
    Qt Code:
    1. 1>Implementation\ImportExport\OpcExporter.cpp(250): error C2893: Failed to specialize function template 'QtPrivate::ReduceResultType<ReduceFunctor>::ResultType QtConcurrent::blockingMappedReduced(Iterator,Iterator,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)'
    2. 1> With the following template arguments:
    3. 1> 'QList<T>::iterator'
    4. 1> with
    5. 1> [
    6. 1> T=QString
    7. 1> ]
    8. 1> 'ImportExport::OpcExporter::CreateProjectChildAndAttachedParts::<lambda_34d77ea4bb1263c3043c2eae1f2efb26>'
    9. 1> 'ImportExport::OpcExporter::CreateProjectChildAndAttachedParts::<lambda_0b977c9c36b310423ccf2f5f1c540fb3>'
    10. 1>Implementation\ImportExport\OpcExporter.cpp(250): error C2783: 'ResultType QtConcurrent::blockingMappedReduced(Iterator,Iterator,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' : could not deduce template argument for 'ResultType'
    11. 1> c:\qt\qt5.0.2\5.0.2\msvc2012_64\include\qtconcurrent\qtconcurrentmap.h(237) : see declaration of 'QtConcurrent::blockingMappedReduced'
    12. 1>Implementation\ImportExport\OpcExporter.cpp(250): error C2893: Failed to specialize function template 'QtPrivate::ReduceResultType<ReduceFunctor>::ResultType QtConcurrent::blockingMappedReduced(const Sequence &,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)'
    13. 1> With the following template arguments:
    14. 1> 'QList<T>::iterator'
    15. 1> with
    16. 1> [
    17. 1> T=QString
    18. 1> ]
    19. 1> 'ImportExport::OpcExporter::CreateProjectChildAndAttachedParts::<lambda_34d77ea4bb1263c3043c2eae1f2efb26>'
    20. 1> 'QList<T>::iterator'
    21. 1> with
    22. 1> [
    23. 1> T=QString
    24. 1> ]
    25. 1>Implementation\ImportExport\OpcExporter.cpp(250): error C2783: 'ResultType QtConcurrent::blockingMappedReduced(const Sequence &,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' : could not deduce template argument for 'ResultType'
    26. 1> c:\qt\qt5.0.2\5.0.2\msvc2012_64\include\qtconcurrent\qtconcurrentmap.h(208) : see declaration of 'QtConcurrent::blockingMappedReduced'
    27. 1>
    28. 1>Build FAILED.
    To copy to clipboard, switch view to plain text mode 

    Any help much appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using lambdas with QtConcurrent::blockingMappedReduced does not compile

    Try calling with the template type explicitly specified, I guess something like

    Qt Code:
    1. WordCount total = QtConcurrent::blockingMappedReduced<WordCount>(files.begin(), files.end(), wordMapFn, wordReduceFn);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    hxcan (17th May 2017)

  4. #3
    Join Date
    Nov 2012
    Posts
    21
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using lambdas with QtConcurrent::blockingMappedReduced does not compile

    Ah of course. Yes that worked. Thanks.

  5. #4
    Join Date
    Nov 2012
    Posts
    21
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using lambdas with QtConcurrent::blockingMappedReduced does not compile

    I spoke too soon:
    Qt Code:
    1. void MyClass::MyFunc() {
    2. typedef QMap<QString, int> WordCount;
    3.  
    4.  
    5. auto wordMapFn = [&](const QString &file) -> WordCount {
    6. QFile f(file);
    7. f.open(QIODevice::ReadOnly);
    8. QTextStream textStream(&f);
    9. WordCount wordCount;
    10.  
    11. while (textStream.atEnd() == false)
    12. foreach (QString word, textStream.readLine().split(" "))
    13. wordCount[word] += 1;
    14.  
    15. return wordCount;
    16. };
    17.  
    18. auto wordReduceFn = [&](WordCount &result, const WordCount &w) {
    19. QMapIterator<QString, int> i(w);
    20. while (i.hasNext()) {
    21. i.next();
    22. result[i.key()] += i.value();
    23. }
    24. };
    25. QStringList files;
    26. WordCount total = QtConcurrent::blockingMappedReduced<WordCount>(files.begin(), files.end(), wordMapFn, wordReduceFn);
    27. }
    To copy to clipboard, switch view to plain text mode 

    gives me this error:

    Qt Code:
    1. 1>c:\qt\qt5.0.2\5.0.2\msvc2012_64\include\qtconcurrent\qtconcurrentmapkernel.h(116): error C2039: 'result_type' : is not a member of 'ImportExport::MyClass::MyFunc::<lambda_fa3d57d9b3ee2c79a98275b7637f0691>' (Implementation\ImportExport\MyClass.cpp)
    2. ...
    To copy to clipboard, switch view to plain text mode 

    The problem is that you can't use the auto keyword for the lambda's or it gets confused. So you have to fully specify the lambdas type:
    Qt Code:
    1. std::function<WordCount (const QString&)> wordMapFn = [&](const QString &file) -> WordCount {
    2. ...
    3. };
    To copy to clipboard, switch view to plain text mode 

    Hope this helps others.

  6. The following 2 users say thank you to gemmell for this useful post:

    jneuhaus20 (3rd June 2015), Phlucious (3rd February 2016)

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using lambdas with QtConcurrent::blockingMappedReduced does not compile

    Could also be a temporary compiler limitation. The C++11 stuff is relatively new.

    Cheers,
    _

  8. #6
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using lambdas with QtConcurrent::blockingMappedReduced does not compile

    For future folks referring to this thread, I found that the std::function<> requirement was definitely and only true if the lambda had a return type, such as for the Mapped functor. Because the return type of the Reduced functor was void, std::function wasn't required.

    I was compiling with MSVC2010.

Similar Threads

  1. QtConcurrent std::bad_alloc
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2013, 06:23
  2. QtConcurrent and QSqlQuery
    By ^NyAw^ in forum Qt Programming
    Replies: 5
    Last Post: 10th April 2013, 14:28
  3. Why is QtConcurrent so slow?
    By ArlexBee-871RBO in forum Qt Programming
    Replies: 10
    Last Post: 29th December 2009, 10:37
  4. qtconcurrent
    By knishaq in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2009, 08:41
  5. QtConcurrent
    By Brandybuck in forum An Introduction to QThreads
    Replies: 2
    Last Post: 9th May 2008, 14:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.