Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++11, can you pass a ref'ed base class to thread's constructor and get polymorphic behavior?

I've observed a difference in behavior between the new library in Visual Studio 11 Beta and Boost with thread() and ref(). I'm wondering who is right. It could be both if the standard deviated from Boost's original implementation. (But I'm not about to try to decipher standardese...)

I would've tried it with MinGW... Alas, AFAIK, <thread> doesn't work for MinGW.

So, first question is, do gcc and Clang exhibit the same compilation failure? If they don't, I'll file a bug against VS. The second question might be, if that compilation failure is correct, what's my workaround to get what Boost gave me (short of keep using Boost)?

And I suppose I do have have a third question... Is what I'm doing even kosher to begin with?

class base
{
public:
    virtual void operator()() = 0;
};

class derived : public base
{
public:
    virtual void operator()()
    {
        cout << "derived" << endl;
    }
};

int main()
{
    base *b = new derived;

    std::thread t(std::ref(*b)); // Nasty compilation errors.

    boost::thread t(boost::ref(*b)); // Works fine.

    t.join();

    return 0;
}
like image 318
screwnut Avatar asked Mar 29 '12 08:03

screwnut


1 Answers

I filed a bug against Visual Studio 11 Beta here. No status yet. Will edit this post with status when I get it.

Edit: Fixed in VS 2015 RTM, as per the update in the bug report.

like image 107
screwnut Avatar answered Oct 15 '22 14:10

screwnut