jeudi 29 mars 2018

Why extra parenthesis is needed to compile "std::thread" functor ?

It is probably a dumb question, I'm moving from the "old" c++ to the newer C++ 11 and looking into the std::thread library.

class MyThread
{
public:
    int i = 0;
    void operator()()
    {
        for (;i < 10000; i++)
            cout << "Exectuing " << endl;
    }
};

In the main I have the following lines:

thread threadObj( MyThread() );

for (int i = 0; i < 1; i++)
    cout << "Main thread " << endl;

threadObj.join();

It won't compile the last line : "Expression must have a class type"

Adding extra parenthesis to : thread threadObj( (MyThread()) ); solves the problem.

Why ? The type remains the same : thread.

Am I missing some new c++ 11 feature ? or just confused.

Aucun commentaire:

Enregistrer un commentaire