create_task function in PPL
-
why is the code giving error while compiling?
task my_task()
{
auto s = make_shared(L"First value");
return create_task([s] {
wcout << *s << endl;
*s = L"Second value";
return *s; // It fails because of this.
}).then([s] {
wcout << *s << endl;
*s = L"Third value";
return *s;
});
}UPDATED The error is as follows,
Quote:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\ppltasks.h(372): error C2338: incorrect parameter type for the callable object in 'then'; consider _ExpectedParameterType or task<_ExpectedParameterType> (see below) 1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ppltasks.h(402): note: see reference to class template instantiation 'Concurrency::details::_FunctionTypeTraits<_Function,_ReturnType>' being compiled 1> with 1> [ 1> _Function=my_task::, 1> _ReturnType=std::wstring 1> ] 1> c:\vc++\concurrency_program\createtask\source.cpp(41): note: see reference to class template instantiation 'Concurrency::details::_ContinuationTypeTraits,_ReturnType>' being compiled 1> with 1> [ 1> _ReturnType=std::wstring 1> ] 1>c:\vc++\concurrency_program\createtask\source.cpp(41): error C2440: 'return': cannot convert from 'Concurrency::task' to 'Concurrency::task' 1> c:\vc++\concurrency_program\createtask\source.cpp(41): note: Constructor for class 'Concurrency::task' is declared 'explicit'
I am just trying to understand ppl programing structure. Where as the following code compiles.
task t1 = create_task([]() {
wprintf(L"create_task\n");
return wstring(L"create_task"); // It doesn't throw any error
});Jesus saves
-
why is the code giving error while compiling?
task my_task()
{
auto s = make_shared(L"First value");
return create_task([s] {
wcout << *s << endl;
*s = L"Second value";
return *s; // It fails because of this.
}).then([s] {
wcout << *s << endl;
*s = L"Third value";
return *s;
});
}UPDATED The error is as follows,
Quote:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\ppltasks.h(372): error C2338: incorrect parameter type for the callable object in 'then'; consider _ExpectedParameterType or task<_ExpectedParameterType> (see below) 1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ppltasks.h(402): note: see reference to class template instantiation 'Concurrency::details::_FunctionTypeTraits<_Function,_ReturnType>' being compiled 1> with 1> [ 1> _Function=my_task::, 1> _ReturnType=std::wstring 1> ] 1> c:\vc++\concurrency_program\createtask\source.cpp(41): note: see reference to class template instantiation 'Concurrency::details::_ContinuationTypeTraits,_ReturnType>' being compiled 1> with 1> [ 1> _ReturnType=std::wstring 1> ] 1>c:\vc++\concurrency_program\createtask\source.cpp(41): error C2440: 'return': cannot convert from 'Concurrency::task' to 'Concurrency::task' 1> c:\vc++\concurrency_program\createtask\source.cpp(41): note: Constructor for class 'Concurrency::task' is declared 'explicit'
I am just trying to understand ppl programing structure. Where as the following code compiles.
task t1 = create_task([]() {
wprintf(L"create_task\n");
return wstring(L"create_task"); // It doesn't throw any error
});Jesus saves
Daniel Ramnath wrote:
why is the code giving error while compiling?
Should we make an attempt at guessing the error?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
why is the code giving error while compiling?
task my_task()
{
auto s = make_shared(L"First value");
return create_task([s] {
wcout << *s << endl;
*s = L"Second value";
return *s; // It fails because of this.
}).then([s] {
wcout << *s << endl;
*s = L"Third value";
return *s;
});
}UPDATED The error is as follows,
Quote:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\ppltasks.h(372): error C2338: incorrect parameter type for the callable object in 'then'; consider _ExpectedParameterType or task<_ExpectedParameterType> (see below) 1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ppltasks.h(402): note: see reference to class template instantiation 'Concurrency::details::_FunctionTypeTraits<_Function,_ReturnType>' being compiled 1> with 1> [ 1> _Function=my_task::, 1> _ReturnType=std::wstring 1> ] 1> c:\vc++\concurrency_program\createtask\source.cpp(41): note: see reference to class template instantiation 'Concurrency::details::_ContinuationTypeTraits,_ReturnType>' being compiled 1> with 1> [ 1> _ReturnType=std::wstring 1> ] 1>c:\vc++\concurrency_program\createtask\source.cpp(41): error C2440: 'return': cannot convert from 'Concurrency::task' to 'Concurrency::task' 1> c:\vc++\concurrency_program\createtask\source.cpp(41): note: Constructor for class 'Concurrency::task' is declared 'explicit'
I am just trying to understand ppl programing structure. Where as the following code compiles.
task t1 = create_task([]() {
wprintf(L"create_task\n");
return wstring(L"create_task"); // It doesn't throw any error
});Jesus saves
This issue is solved when 'then' function call is parameterized as follows
task my_task()
{
auto s = make_shared(L"First value");
return create_task([s] { // 2. Compilers show the error here.
wcout << *s << endl;
*s = L"Second value";
return *s; // 3. The error was due to this.
}).then([](wstring s) { // 1. Actually the fix is here.
wcout << s << endl;
s = L"Third value";
return s;
});
}Jesus saves
-
Daniel Ramnath wrote:
why is the code giving error while compiling?
Should we make an attempt at guessing the error?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Sorry, my description was not elaborate. I updated the error. But I found out the reason and posted it below.
Jesus saves