Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does std::runtime_error copy the string passed in the constructor?

Tags:

c++

exception

I wonder if this line creates a dangling pointer:

string arg="derp";
throw std::runtime_error("Unknown argument "+arg);

Does std::runtime_error copy the string, or does it store the reference?

like image 525
Lorenzo Pistone Avatar asked May 17 '12 23:05

Lorenzo Pistone


1 Answers

std::exception (which std::runtime_error inherits from) must make a copy of the message, even if that's not called out in the standard explicitly (that I can find). There's no reason for the exception class to expect that the source of the what() message will outlive it in the general case.

like image 92
Michael Burr Avatar answered Oct 13 '22 01:10

Michael Burr