>
>
>
V1017. Variable of the 'string_view' ty…


V1017. Variable of the 'string_view' type references a temporary object, which will be removed after evaluation of an expression.

The analyzer has detected that an instance of class 'std::string_view' is initialized by or assigned a temporary object.

Consider the following example:

std::string hello = "Hello, ";
std::string_view helloWorldPtr = hello + "world\n";
std::cout << helloWorldPtr;

In the second line of this code, a temporary object of the type 'std::string' will be created, the pointer to which will be copied when initializing the instance of the 'std::string_view' class. After that, once the initialization expression has been evaluated, the temporary object will be destroyed; therefore, the pointer used in the third line will be referring to a freed memory block.

This is the fixed version:

std::string hello = "Hello, ";
const std::string helloWorld = hello + "world\n";
std::string_view helloWorldPtr = helloWorld;
std::cout << helloWorldPtr;

This diagnostic is classified as: