Using functor disables creating threads through default constrcutor.

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.
The line

 
thread t( doer() );


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
Thanks a lot, that explains it.
closed account (EzwRko23)
And let someone tell me C++ syntax is simple and readable :D
Why are you even here, xorebxebx?
Why are you even here, xorebxebx?
To troll, wasn't that clear enough?
I guess I shouldn't be surprised. =P

It just seems to be getting more and more blatant.
Topic archived. No new replies allowed.