using_virtual_inheritance_in_exception_types.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
  2. 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  4. <head>
  5. <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
  6. <title>using virtual inheritance in exception types</title>
  7. <link href='reno.css' type='text/css' rel='stylesheet'/>
  8. </head>
  9. <body>
  10. <div class="body-0">
  11. <div class="body-1">
  12. <div class="body-2">
  13. <div>
  14. <div id="boost_logo">
  15. <a href="http://www.boost.org"><img style="border:0" src="../../../boost.png" alt="Boost" width="277" height="86"/></a>
  16. </div>
  17. <h1>Boost Exception</h1>
  18. </div>
  19. <!-- Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. -->
  20. <!-- Distributed under the Boost Software License, Version 1.0. (See accompanying -->
  21. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  22. <div class="RenoIncludeDIV"><div class="RenoAutoDIV"><h3>Using Virtual Inheritance in Exception Types</h3>
  23. </div>
  24. <p>Exception types should use virtual inheritance when deriving from other exception types. This insight is due to Andrew Koenig. Using virtual inheritance prevents ambiguity problems in the exception handler:</p>
  25. <pre>#include &lt;iostream&gt;
  26. struct my_exc1 : std::exception { char const* what() const throw(); };
  27. struct my_exc2 : std::exception { char const* what() const throw(); };
  28. struct your_exc3 : my_exc1, my_exc2 {};
  29. int
  30. main()
  31. {
  32. try { throw your_exc3(); }
  33. catch(std::exception const&amp; e) {}
  34. catch(...) { std::cout &lt;&lt; "whoops!" &lt;&lt; std::endl; }
  35. }</pre>
  36. <p>The program above outputs "whoops!" because the conversion to std::exception is ambiguous.</p>
  37. <p>The overhead introduced by virtual inheritance is always negligible in the context of exception handling. Note that virtual bases are initialized directly by the constructor of the most-derived-type (the type passed to the throw statement, in case of exceptions.) However, typically this detail is of no concern when boost::<span class="RenoLink"><a href="exception.html">exception</a></span> is used, because it enables exception types to be trivial structs with no members (there's nothing to initialize.) See <span class="RenoLink"><a href="exception_types_as_simple_semantic_tags.html">Exception Types as Simple Semantic Tags</a></span>.</p>
  38. </div><div class="RenoAutoDIV"><div class="RenoHR"><hr/></div>
  39. See also: <span class="RenoPageList"><a href="boost-exception.html">Boost Exception</a>&nbsp;| <a href="frequently_asked_questions.html">Frequently Asked Questions</a></span>
  40. </div>
  41. <!-- Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. -->
  42. <!-- Distributed under the Boost Software License, Version 1.0. (See accompanying -->
  43. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  44. <div id="footer">
  45. <p>
  46. <a class="logo" href="http://jigsaw.w3.org/css-validator/check/referer"><img class="logo_pic" src="valid-css.png" alt="Valid CSS" height="31" width="88"/></a>
  47. <a class="logo" href="http://validator.w3.org/check?uri=referer"><img class="logo_pic" src="valid-xhtml.png" alt="Valid XHTML 1.0" height="31" width="88"/></a>
  48. <small>Copyright (c) 2006-2009 by Emil Dotchevski and Reverge Studios, Inc.<br/>
  49. Distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License, Version 1.0</a>.</small>
  50. </p>
  51. </div>
  52. </div>
  53. </div>
  54. </div>
  55. </body>
  56. </html>