简体   繁体   English

临时对象的生存期:在嵌套函数调用中迭代到临时向量

[英]Lifetime of temporary object: iterator to temporary vector in nested function call

Let's suppose that I have such classes: 让我们假设我有这样的类:

struct QString {
    //return null terminated array
    std::vector<char> toLocal8Bit() const;
};

struct string_view {
    const char *data;
    size_t len;
    string_view(const char *str): data(str), len(std::strlen(str)) {}
};

and I have function with such signature: 并且我具有这样的签名功能:

void f(const string_view& str);

Is it valid code 它是有效的代码吗

QString str;
f(string_view(&*str.toLocal8Bit().begin()));

?

I mean when temporary std::vector will be destroyed? 我的意思是什么时候临时std :: vector将被销毁?

The temporary is destroyed at the end of the full statement. 临时语句在完整语句的末尾被销毁。 So this is safe. 所以这很安全。

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

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