简体   繁体   English

experimental :: basic_string_view <>是否适用于rvalues?

[英]Does experimental::basic_string_view<> work with rvalues?

I am not 100% that the following code is semantically correct: 我不是100%以下代码在语义上是正确的:

#include <iostream>
#include <experimental/string_view>

int main()
{
    std::string str = "lvalue string";

    std::experimental::string_view view_lvalue(str);
    std::experimental::string_view view_rvalue(std::string{"rvalue string"});

    std::cout << view_lvalue << '\n' << view_rvalue << '\n';
}

Live on Wandbox 住在Wandbox上

Question: Can I legally bind a rvalue to std::experimental::basic_string_view , or is it just UB? 问题:我可以合法地将rvalue绑定到std :: experimental :: basic_string_view ,还是只是UB? If yes, how does it work? 如果是,它是如何工作的? As far as I know, a rvalue does not bind to a const reference (which I assume the view holds to the original string) via the constructor, so I thought that at the end of the statement std::experimental::string_view view_rvalue(std::string{"rvalue string"}); 据我所知,rvalue不会通过构造const绑定到const引用(我假设视图保持原始字符串),所以我认为在语句结束时std::experimental::string_view view_rvalue(std::string{"rvalue string"}); the reference will be dangling. 引用将是悬空。 Does string_view use a more sophisticated approach? string_view是否使用更复杂的方法?

I am asking this because I am trying to write a similar view for some matrix class, and don't yet know how to deal with rvalues (I can disable them of course, but I don't think it's the best approach). 我问这个是因为我试图为一些矩阵类写一个类似的视图,并且还不知道如何处理rvalues(我当然可以禁用它们,但我不认为这是最好的方法)。

If cpprefernce is correct then this is UB. 如果cpprefernce是正确的,那么这是UB。 std::string_view has std::string_view

A typical implementation holds only two members: a pointer to constant CharT and a size . 典型的实现仅包含两个成员:指向常量CharTsize的指针。

And the constructor has 构造函数有

Constructs a view of the first str.size() characters of the character array starting with the element pointed by str.data() . 构造一个以str.data()指向的元素开头的字符数组的第一个str.size()字符的视图。

So if string_view just points to the underlying char array of the provided string then we will have a dangling pointer once the expression ends and the temporary is destroyed. 因此,如果string_view只指向提供的字符串的基础char数组,那么一旦表达式结束并且临时被销毁,我们将有一个悬空指针。

As pointed out in the comments one reason this behavior may have been allowed is so you can pass a string_view to a function and construct that string_view from a temporary string 正如评论中所指出的,这种行为可能被允许的一个原因是你可以将string_view传递给一个函数并从一个临时string构造string_view

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C++17 中没有从 std::string 到 std::string_view 的隐式转换(在 std::experimental::basic_string_view 中) - No implicit conversion from std::string to std::string_view in C++17 (was in std::experimental::basic_string_view) C ++ 17 std :: basic_string_view是否会使C字符串的使用无效? - Does C++17 std::basic_string_view invalidates the use of C strings? 为什么 std::basic_string_view 有两个相等比较运算符? - Why does std::basic_string_view have two equality comparison operators? 为什么 basic_string_view 不限于字符类型? - Why is basic_string_view not limited to character types? 表示为std :: basic_string_view的迭代正则表达式子匹配 - Iterating regex submatches represented as std::basic_string_view 如何自动推断类型为“basic_string_view”等的 function 参数的模板参数? - How to automaticly deduce template parameter for a function parameter of type `basic_string_view` or the like? std::basic_string_view 之间的区别<t>和 std::span<t></t></t> - Difference between std::basic_string_view<T> and std::span<T> 如何让模板 function 接受任何你可以构造一些 basic_string_view 的东西 - How to let a template function accept anything you can construct some basic_string_view out libc ++是否为太多basic_string_view提供了哈希专用化? - Is libc++ providing hash specialization for too many basic_string_view's? C2039 C2228 'get_value_or': 不是 'network::basic_string_view 的成员<char,std::char_traits<char> &gt;' </char,std::char_traits<char> - C2039 C2228 'get_value_or': is not a member of 'network::basic_string_view<char,std::char_traits<char>>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM