how std::thread constructor detects rvalue reference?

假如想象 提交于 2019-12-01 11:16:57
auto s = std::decay_copy(std::string("hello"));

Is equivalent to:

template<>
std::string std::decay_copy<std::string>(std::string&& src) {
    return std::string(std::move(src));
}

std::string s = decay_copy<std::string>(std::string("hello"));

The std::thread constructor knows the value category of its arguments, because it knows what Function and Args... are, which it uses to perfectly forward the its parameters to decay_copy (or equivalent).

The actual thread function doesn't know the value category. It's always invoked as an rvalue, with all rvalue arguments - which makes sense: the copies of f and args... are local to the thread, and won't be used anywhere else.

It is common problem of the perfect forwarding. If you want to restore information about rvalue in the function, you have to use std::forward std::forward . If you are interested in the value type detection you may read this value_category . From the description you can find the information how the compiler recognizes rvalue, xvalue, lvalue, prvalue, gvalue on compile time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!