I'm trying to use parallel_for, but I get an error, the code is:

#include "stdafx.h"

#include <iostream>
#include <windows.h>
#include <ppl.h>

using namespace std;
using namespace concurrency;

int _tmain(int argc, _TCHAR* argv[])
{
    parallel_for(size_t(0), 50, [&](size_t i)
    {
        cout << i << ",";
    }

    cout << endl;

    getchar();
    return 0;
}

The error is:

IntelliSense: no instance of overloaded function "parallel_for" matches the argument list >argument types are: (size_t, int, lambda []void (size_t i)->void

This is only example, I have to use it in bigger project, but first of all I want to understand how to use it properly.

*edit*

i changed the code to:

parallel_for(size_t(0), 50, [&](size_t i)
{
    cout << i << ",";
});

but still i get the annoying error: IntelliSense: no instance of overloaded function "parallel_for" matches the argument list argument types are: (size_t, int, lambda []void (size_t i)->void)

有帮助吗?

解决方案

parallel_for has the prototype

template <typename T, typename F>
void parallel_for(
   T first,
   T last,
   F& f,
   const auto_partitioner& _Part = auto_partitioner()
);

T is deduced from first and last but you're giving it size_t and int making T ambiguous.

Also, there are other overloaded functions parallel_for and MSVC generates a stupid error message in such case.

Solution 1 :

int _tmain(int argc, _TCHAR* argv[])
{
    parallel_for(size_t(0), size_t(50), [&](size_t i)
    {
        cout << i << ",";
    });

    cout << endl;

    getchar();
    return 0;
}

Solution 2 :

parallel_for<size_t>(size_t(0), 50, [&](size_t i)
        {
            cout << i << ",";
        });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top