小编D3V*_*1R3的帖子

函数参数究竟何时被破坏?

我有一个问题,因为我不清楚函数参数何时被销毁。因此,以下 doSomething 函数的串联是否容易出错?

我问是因为“程序员有责任确保 std::string_view 的寿命不会超过指向的字符数组”。在特定情况下可以保证吗?

#include <string>
#include <string_view>

std::string doSomething(const std::string_view& str_view)
{
    // do something and create a new std::string instance based on the std::string_view instance

    return str;
}

int main()
{
    std::string input_str{"Hello world!"};

    std::string output_str{ doSomething(doSomething(doSomething(input_str))) };

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ destructor arguments string-view c++17

5
推荐指数
1
解决办法
82
查看次数

标签 统计

arguments ×1

c++ ×1

c++17 ×1

destructor ×1

string-view ×1