Github user mreve commented on a diff in the pull request:
https://github.com/apache/thrift/pull/469#discussion_r29559030
--- Diff: compiler/cpp/src/generate/t_cpp_generator.cc ---
@@ -1699,6 +1723,24 @@ void
t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out, t_str
out << "}" << endl << endl;
}
+/**
+ * Generates what() method for exceptions
+ */
+void t_cpp_generator::generate_exception_what_method(std::ofstream& out,
t_struct* tstruct) {
+ out << indent();
+ generate_exception_what_method_decl(out, tstruct, true);
+ out << " {" << endl;
+
+ indent_up();
+
+ out << indent() << "std::stringstream ss;" << endl;
+ out << indent() << "ss << \"TException - service has thrown: \" <<
*this;" << endl;
+ out << indent() << "return ss.str().c_str();" << endl;
--- End diff --
Hey, thanks for help! However, before I begin to write it - I believe it
can cause sometimes SegFault even if it would pass the unit test. And, what
more, we'll encounter this problem no matter what we return here (any string
created in what() will be destroyed the same way as the ss, won't it?).
So, the only way to make it work is to store the returned text in a field
of the custom exception class, am I right? Then we can return a pointer to this
field, and it will remain until the exception is not destroyed. BUT - what is a
const function. So, I can't modify the object itself inside what().
There is a tricky way to get around it...we can keep a mutable variable as
a data member field. Then in what() we can generate the message, store it in
mutable field (which we can modify no matter const function), and return the
pointer to this field. But again, if somebody calls what() twice, the second
call will override the message so the first obtained pointer can become
invalid... And another thing, this mutable field, what type should it be?
String, and we'd return .c_str() from it?
What do you think would be a solution? I understand that the scenarios I'm
talking about aren't very typical for the exceptions, but I think they still
should be discussed...
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---