Код: Выделить всё
struct MyExcept : std::exception {
explicit MyExcept(const char* m) noexcept : message{m} {}
const char* what() const noexcept override {
return message;
}
const char* message;
};
void foo() {
std::string error;
error += "Some";
error += " Error";
throw MyExcept{error.c_str()};
}
int main() {
try {
foo();
} catch (const MyExcept& e) {
// Is this okay?
std::cout в консоли и прерывать программу. Так это все еще неопределенное поведение?
Подробнее здесь: [url]https://stackoverflow.com/questions/45220063/is-this-use-of-c-str-with-exception-undefined-behavior[/url]