Using functor disables creating threads through default constrcutor.
Oct 29, 2010 at 8:52am UTC
While using functors to create threads, I faced this peculiar problem. The following method of invoking thread doesn't work.
using namespace boost;
class doer{
public:
void operator() () {
std::cout<<"\n Are you trying to do something that you don't understand\n";
}
};
int _tmain(int argc, _TCHAR* argv[])
{
thread t(doer());
t.join(); // Throws the error noted below
return 0;
}
error C2228: left of '.join' must have class/struct/union
I verified from the intellisense(visual c++), and the type of t comes out to be
boost::thread t(doer(*)(void))
While if I replace the defualt constrctuor call with
thread t(*(new doer()))
the code compiles and runs clean.
I am baffled by it. Can someone please help me out with this.
Oct 29, 2010 at 1:28pm UTC
The line
does not declare a variable.
thread t = thread( doer() );
does.
Google "C++ most vexing parse" for details.
[EDIT: It declares a funtion named "t" that returns a thread and takes, as its single parameter, another function
that returns a doer and takes no parameters.]
Last edited on Oct 29, 2010 at 1:30pm UTC
Nov 2, 2010 at 8:55am UTC
Thanks a lot , that explains it.
Nov 2, 2010 at 4:23pm UTC
And let someone tell me C++ syntax is simple and readable :D
Nov 2, 2010 at 8:20pm UTC
Why are you even here, xorebxebx?
Nov 2, 2010 at 10:11pm UTC
Why are you even here, xorebxebx?
To troll, wasn't that clear enough?
Nov 2, 2010 at 10:49pm UTC
I guess I shouldn't be surprised. =P
It just seems to be getting more and more blatant.
Topic archived. No new replies allowed.