c++ - How to supplement boost::exception with a proper what() function -


i boost::exception quite bit, i'm quite bothered not provide proper what() function out of box. don't confused, have nice boost::diagnostic_information contains information see in hypothetic what() function since boost::exception not inherit std::exception what() function if multiple inherit (as suggested tutorial, see line below) default useless what() std::exception base explains nothing exception.

struct my_exception: virtual std::exception, virtual boost::exception { }; 

now tried override what() , make return boost::diagnostic_information somehow not work, i'm bit puzzled. might because loop i'm not quite sure.

ps: reason want implement what() right is shown default lot of tools if program dies them (e.g. gnu compiler show nice fatal error, , display what(), boost unit tests tools etc.).

here's link test code below

#include <boost/exception/all.hpp>  struct my_exception: virtual std::exception, virtual boost::exception {};  struct my_exception2: virtual std::exception, virtual boost::exception {     virtual const char* what() const throw() {         return "what";     } };  struct my_exception3: virtual std::exception, virtual boost::exception {     virtual const char* what() const throw() {         return boost::diagnostic_information(this).c_str();     } };  int main() {     try {         boost_throw_exception(my_exception());     } catch (const std::exception& e){         std::cout << e.what() << std::endl;          //this useless ___ std::exception     }      try {         boost_throw_exception(my_exception());     } catch (const boost::exception& e){         std::cout << boost::diagnostic_information(e) << std::endl;          //this i'd see ___ main.cpp(39): throw in function int main() ___ dynamic exception type: boost::exception_detail::clone_impl ___ std::exception::what: std::exception     }      try {         boost_throw_exception(my_exception2());     } catch (const std::exception& e){         std::cout << e.what() << std::endl;          //overriding works ___     }      try {         boost_throw_exception(my_exception3());     } catch (const std::exception& e){         std::cout << e.what() << std::endl;          //but somehow here not work ___ unknown exception.     } } 

first, boost::diagnostic_information takes exception (const) reference, , this pointer:

    return boost::diagnostic_information(*this).c_str();                                          ^-- here 

second, once you've fixed that, you've correctly anticipated results in infinite recursion boost::diagnostic_information calls std::exception::what(). possible work around guard member or similar:

struct my_exception3: std::exception, boost::exception {     mutable bool in_what = false;     virtual const char* what() const throw() {         struct g { bool &b; ~g() { b = false; } } guard{in_what};         return in_what ? "what" : (in_what = true, boost::diagnostic_information(*this).c_str());     } }; 

finally, you're using c_str destructed temporary string. i'll leave solution problem exercise.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -