c++ - 将 std::string 的 xvalue 传递给采用 std::string_view 的函数

标签 c++ c++17 string-view

做下面的事情是UB吗?

void foo(std::string_view view) {...}

void bar()
{
   std::string str;
   foo(std::move(str));
}

谢谢!

最佳答案

这是定义明确的行为,尽管它不是特别有用。 basic_stringoperator basic_string_view 将用于执行转换,它没有左值/右值限制。因此,如果您根本没有使用 move,它会做同样的事情。

关于c++ - 将 std::string 的 xvalue 传递给采用 std::string_view 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41836289/

相关文章:

c++ - 编程语言包装器

c++ - 用空格分隔字符串的最简单方法?

c++ - 我可以在模板参数中声明一个 constexpr lambda 吗?

c++ - 结构化绑定(bind)和基于范围的;在 gcc 中抑制未使用的警告

c++ - C++17 std::shared_mutex 还不可用吗?

c++ - 为什么string_view没有推导指南?

c++ - 添加新继承的 "interface"和虚拟方法需要重新编译

c++ - 如何读/写或迭代 std::array 中特定范围的元素?

c++ - 如何在 constexpr string_view 上使用 std::string_view::remove_prefix()

c++ - 当参数被转发时,std::move 在参数列表中是否安全,而不是 move 构造?