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

Passing xvalue of std::string to function taking std::string_view

做下面的事情是UB吗?

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

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

谢谢!

这是 well-defined 行为,尽管它不是特别有用。 basic_stringoperator basic_string_view 将用于执行转换,它没有 lvalue/rvalue 限制。所以它会做同样的事情,如果你根本没有使用 move