'Saving const ref string in std::pair

Example code:

constexpr const char* tstStr = "Test str";

void testFunc(std::pair<const std::string&, int> el)
{
    std::cout << "Test func called" << el.first << std::endl;
}

int main()
{
    testFunc({std::string(tstStr), 1}); // Ok
    testFunc({tstStr, 1}); // Corrupt

}

The testFunc({std::string(tstStr), 1}); line finishes successfully, but the next line fails (asan confirms corruption)

Can someone explain what is happening?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source