简体   繁体   中英

Dereferencing a copying the value of a unique_ptr

I have a function that is given a unique_ptr to a string. I want to copy that value is lambdas and use it to invoke functions that take std::string. Is what I am doing below the right way to do this:

void someFunc(std::unique_ptr<std::string> id) {
    const std::string idStr = *id;
    stream.subscribe([=](auto arg1) { arg1.invoke(idStr); });
}

What I want to do is just copy the string into a new location. Don't care about the id unique_ptr at that point. I found this discussion that says that seems to indicate that I can do the following:

std::string idStr = std::move(id);

However, that does't actually work for me. It fails with "no viable conversion" error.

I have a function that is given a unique_ptr to a string. I want to copy that value

void someFunc(std::unique_ptr<std::string> id) { const std::string idStr = *id;

What I want to do is just copy the string into a new location.

That is a correct way to copy the pointed string, yes.

... that I can do the following:

 std::string idStr = std::move(id);

That is ill-formed. You cannot do that. The examples in the linked question do not do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM