取消引用 a 复制 unique_ptr 的值
Dereferencing a copying the value of a unique_ptr
我有一个函数,它给一个字符串 unique_ptr。我想复制该值是 lambda 并使用它来调用采用 std::string 的函数。我在下面做的是正确的方法吗:
void someFunc(std::unique_ptr<std::string> id) {
const std::string idStr = *id;
stream.subscribe([=](auto arg1) { arg1.invoke(idStr); });
}
我想做的只是将字符串复制到一个新位置。那时不要关心 id unique_ptr 。我发现 this discussion 似乎表明我可以执行以下操作:
std::string idStr = std::move(id);
但是,这实际上对我不起作用。它因 "no viable conversion" 错误而失败。
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 I can do the following:
std::string idStr = std::move(id);
即ill-formed。你不能这样做。链接问题中的示例不会那样做。
我有一个函数,它给一个字符串 unique_ptr。我想复制该值是 lambda 并使用它来调用采用 std::string 的函数。我在下面做的是正确的方法吗:
void someFunc(std::unique_ptr<std::string> id) {
const std::string idStr = *id;
stream.subscribe([=](auto arg1) { arg1.invoke(idStr); });
}
我想做的只是将字符串复制到一个新位置。那时不要关心 id unique_ptr 。我发现 this discussion 似乎表明我可以执行以下操作:
std::string idStr = std::move(id);
但是,这实际上对我不起作用。它因 "no viable conversion" 错误而失败。
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 I can do the following:
std::string idStr = std::move(id);
即ill-formed。你不能这样做。链接问题中的示例不会那样做。