Uniform initialization allows passing an object to class' constructor. Why?

Viewed 35

This title might be a little confusing, but I am hurrying with the explanation.

I wanted to ask you guys why having a class like that:

class scoped_thread2
{
    std::thread t;
public:
    explicit scoped_thread2(std::thread t_) : t(std::move(t_))
    {
    }

    ~scoped_thread2() {
        t.join();
    }
};

I was calling constructor of scoped_thread2 like this:

scoped_thread2 thread_test{ std::thread(do_something) };

Where do_something was a simple void function that actually did nothing.

void do_something()
{
    return;
}

So I'm asking, why the first initialization works, and I clearly see in Visual Studio's debugger that we are going into scoped_thread2's constructor, but in this case:

scoped_thread2 thread_test2(std::thread(do_something));

It does not work. I found that it's somehow related to uniform initialization that was introduced in C++11, but I couldn't find a good explanation that would explain this difference to me clearly. Anyone?

0 Answers
Related