<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
<title>Deprecating Exception Specifications</title>
<meta name="author" content="Doug Gregor" />
<meta name="date" content="2010-03-12" />
<style type="text/css">

/*
:Author: David Goodger
:Contact: goodger@python.org
:Date: $Date: 2006-05-21 16:44:42 -0400 (Sun, 21 May 2006) $
:Revision: $Revision: 4564 $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.

See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/

/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
  border: 0 }

table.borderless td, table.borderless th {
  /* Override padding for "table.docutils td" with "! important".
     The right padding separates the table cells. */
  padding: 0 0.5em 0 0 ! important }

.first {
  /* Override more specific margin styles with "! important". */
  margin-top: 0 ! important }

.last, .with-subtitle {
  margin-bottom: 0 ! important }

.hidden {
  display: none }

a.toc-backref {
  text-decoration: none ;
  color: black }

blockquote.epigraph {
  margin: 2em 5em ; }

dl.docutils dd {
  /* margin-bottom: 0.5em */
  margin: 0 0 0 22px; }

/* Uncomment (and remove this text!) to get bold-faced definition list terms*/
dl.docutils dt {
  font-weight: bold; 
  margin: 2em 0 0 22px;
  line-height: 1.8em;
   }


div.abstract {
  margin: 2em 5em }

div.abstract p.topic-title {
  font-weight: bold ;
  text-align: center }

div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

/* Uncomment (and remove this text!) to get reduced vertical space in
   compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
  margin-bottom: 0.5em }

div.compound .compound-last, div.compound .compound-middle {
  margin-top: 0.5em }
*/

div.dedication {
  margin: 2em 5em ;
  text-align: center ;
  font-style: italic }

div.dedication p.topic-title {
  font-weight: bold ;
  font-style: normal }

div.figure {
  margin-left: 2em ;
  margin-right: 2em }

div.footer, div.header {
  clear: both;
  font-size: smaller }

div.line-block {
  display: block ;
  margin-top: 1em ;
  margin-bottom: 1em;
  margin-left: 22px; }

div.line-block div.line-block {
  margin-top: 0 ;
  margin-bottom: 0 ;
  /* margin-left: 1.5em; */
  margin-left: 22px; }

div.sidebar {
  margin-left: 1em ;
  border: medium outset ;
  padding: 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

div.system-messages {
  margin: 5em }

div.system-messages h1 {
  color: red }

div.system-message {
  border: medium outset ;
  padding: 1em }

div.system-message p.system-message-title {
  color: red ;
  font-weight: bold }

div.topic {
  margin: 2em }

h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  margin-top: 0.4em }

h1.title {
  text-align: center }

h2.subtitle {
  text-align: center }

hr.docutils {
  width: 75% }

img.align-left {
  clear: left }

img.align-right {
  clear: right }

ol.simple, ul.simple {
  margin-bottom: 1em }

ol.arabic {
  list-style: decimal }

ol.loweralpha {
  list-style: lower-alpha }

ol.upperalpha {
  list-style: upper-alpha }

ol.lowerroman {
  list-style: lower-roman }

ol.upperroman {
  list-style: upper-roman }

p.attribution {
  text-align: right ;
  margin-left: 50% }

p.caption {
  font-style: italic }

p.credits {
  font-style: italic ;
  font-size: smaller }

p.label {
  white-space: nowrap }

p.rubric {
  font-weight: bold ;
  font-size: larger ;
  color: maroon ;
  text-align: center }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

p.topic-title {
  font-weight: bold }

pre.address {
  margin-bottom: 0 ;
  margin-top: 0 ;
  font-family: serif ;
  font-size: 100% }

pre.literal-block, pre.doctest-block {
  margin-left: 2em ;
  margin-right: 2em }

span.classifier {
  font-family: sans-serif ;
  font-style: oblique }

span.classifier-delimiter {
  font-family: sans-serif ;
  font-weight: bold }

span.interpreted {
  font-family: sans-serif }

span.option {
  white-space: nowrap }

span.pre {
  white-space: pre }

span.problematic {
  color: red }

span.section-subtitle {
  /* font-size relative to parent (h1..h6 element) */
  font-size: 80% }

table.citation {
  border-left: solid 1px gray;
  margin-left: 1px }

table.docinfo {
  margin: 2em 4em }

table.docutils {
  /*margin-top: 0.5em ;
  margin-bottom: 0.5em;*/
  margin: 0 0 0 22px; }

table.footnote {
  border-left: solid 1px black;
  margin-left: 1px }

table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
  padding-left: 0.5em ;
  padding-right: 0.5em ;
  vertical-align: top }

table.docutils th.field-name, table.docinfo th.docinfo-name {
  font-weight: bold ;
  text-align: left ;
  white-space: nowrap ;
  padding-left: 0 }

h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  font-size: 100% }

ul.auto-toc {
  list-style-type: none }

.ins {background-color:#A0FFA0;text-decoration:underline}
.del {background-color:#FFA0A0;text-decoration:line-through}
.ed {background-color:#FFFF00}

.grammar { padding: 0 }

.sub {
	position: relative;
	bottom: -0.5em;
	font-size: 0.8em;
}

</style>
</head>
<body>
<div class="document" id="deprecating-exception-specifications">
<h1 class="title">Deprecating Exception Specifications</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Doug Gregor</td></tr>
<tr><th class="docinfo-name">Contact:</th>
<td><a class="first last reference external" href="mailto:doug.gregor&#64;gmail.com">doug.gregor&#64;gmail.com</a></td></tr>
<tr><th class="docinfo-name">Date:</th>
<td>2010-03-12</td></tr>
<tr class="field"><th class="docinfo-name">Number:</th><td class="field-body">N3051=10-0041</td>
</tr>
</tbody>
</table>
<!-- build HTML with:

rst2html.py - -footnote-references=superscript \
  - -stylesheet-path=./rst.css - -embed-stylesheet throwing-move.rst \
  N3051.html -->
<div class="contents topic" id="index">
<p class="topic-title first">index</p>
<ul class="simple">
<li><a class="reference internal" href="#introduction" id="id4">Introduction</a></li>
<li><a class="reference internal" href="#approach" id="id5">Approach</a></li>
<li><a class="reference internal" href="#proposed-changes-to-standard-wording" id="id6">Proposed Changes to Standard Wording</a><ul>
<li><a class="reference internal" href="#exception-specifications-except-spec" id="id7">15.4 Exception specifications [except.spec]</a></li>
<li><a class="reference internal" href="#d-5-dynamic-exception-specifications-depr-except-spec" id="id8"><span class="ins">D.5 Dynamic exception specifications [depr.except.spec]</span></a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="introduction">
<h1><a class="toc-backref" href="#id4">Introduction</a></h1>
<dl class="docutils">
<dt>UK-136</dt>
<dd>Exception specifications have proven close to worthless in practice, while adding a measurable overhead to programs. The feature should be deprecated. The one exception to the rule is the empty throw specification which could serve a legitimate optimizing role if the requirement to call the runtime unexpected mechanism was relaxed in this case.</dd>
</dl>
<p>As expressed in the national body comment above, exception
specifications have not proven useful in practice. There are numerous
discussions of the problems with exception specifications in C++ (see,
e.g., <a class="citation-reference" href="#sutter02" id="id1">[Sutter02]</a>, <a class="citation-reference" href="#boost03" id="id2">[Boost03]</a>), but the main issues are:</p>
<ul class="simple">
<li><em>Run-time checking</em>: C++ exception specifications are checked at runtime
rather than at compile time, so they offer no programmer guarantees
that all exceptions have been handled. The run-time failure mode
(calling <tt class="docutils literal"><span class="pre">std::unexpected()</span></tt>) does not lend itself to recovery.</li>
<li><em>Run-time overhead</em>: Run-time checking requires the compiler to
produce additional code that also hampers optimizations.</li>
<li><em>Unusable in generic code</em>: Within generic code, it is not generally
possible to know what types of exceptions may be thrown from
operations on template arguments, so a precise exception
specification cannot be written.</li>
</ul>
<p>In practice, only two forms of exception-throwing guarantees are
useful: an operation might throw an exception (any exception) or an
operation will never throw any exception. The former is expressed by
omitting the exception-specification entirely, while the latter <em>can</em> be
expressed as <tt class="docutils literal"><span class="pre">throw()</span></tt> but rarely is, due to performance
considerations.</p>
<p><a class="citation-reference" href="#n3050" id="id3">[N3050]</a> introduces a new kind of exception specification, <tt class="docutils literal"><span class="pre">noexcept</span></tt>,
the specifies that the function will not throw any exceptions. Unlike
<tt class="docutils literal"><span class="pre">throw()</span></tt>, <tt class="docutils literal"><span class="pre">noexcept</span></tt> does not require the compiler to introduce
code to check whether an exception is thrown. Rather, if a function
specified as <tt class="docutils literal"><span class="pre">noexcept</span></tt> is exited via an exception, the result is
a call to <tt class="docutils literal"><span class="pre">std::terminate()</span></tt>.</p>
<p>With the introduction of <tt class="docutils literal"><span class="pre">noexcept</span></tt>, programmers can now express the
two kinds of exception guarantees that are useful in practice, without
additional overhead. This paper therefore proposes to deprecate
&quot;dynamic&quot; exception specifications, i.e., those that are written as
<span class="raw-html"><code>throw(</code><i>type-id-list<sub>opt</sub></i><code>)</code></span>.</p>
</div>
<div class="section" id="approach">
<h1><a class="toc-backref" href="#id5">Approach</a></h1>
<p>To aid in the transition from dynamic exception specifications to
<tt class="docutils literal"><span class="pre">noexcept</span></tt>, the wording provides somewhat loose compatibility rules
for redeclarations of functions that have exception
specifications. Two rules stand out:</p>
<blockquote>
<p>1) All &quot;non-throwing&quot; forms of exception specifications
(<tt class="docutils literal"><span class="pre">throw()</span></tt>, <tt class="docutils literal"><span class="pre">noexcept</span></tt>, <tt class="docutils literal"><span class="pre">noexcept(true)</span></tt>) are considered
compatible, but the exception specification on the definition is
what affects code generation:</p>
<pre class="literal-block">
// header ultramodern.h
void f() noexcept;

// source plodding.cpp
#include &quot;ultramodern.h&quot;
struct X { };
void f() throw() { // okay, compatible with noexcept
  throw X(); // calls std::unexpected()
}
</pre>
<p>2) noexcept(false) is considered compatible with <tt class="docutils literal"><span class="pre">throw(</span></tt>
<em>type-id-list</em> <tt class="docutils literal"><span class="pre">)</span></tt>:</p>
<pre class="literal-block">
// header ultramodern.h
void g() noexcept(false);

// source plodding.cpp
#include &quot;ultramodern.h&quot;
struct X { };
void g() throw(X) { // okay, compatible with noexcept(false)
  throw X(); // okay
}
</pre>
</blockquote>
<p>These compatibility rules allow a gradual migration from dynamic
exception specifications to <tt class="docutils literal"><span class="pre">noexcept</span></tt>, since a declaration of a
function can choose to use the new or old syntax independently in the
declaration and in the definition.</p>
</div>
<div class="section" id="proposed-changes-to-standard-wording">
<h1><a class="toc-backref" href="#id6">Proposed Changes to Standard Wording</a></h1>
<p>The wording in this paper is based on the current working paper
(N3035) as amended by N3050.</p>
<div class="section" id="exception-specifications-except-spec">
<h2><a class="toc-backref" href="#id7">15.4 Exception specifications [except.spec]</a></h2>
<p>Modify the paragraphs in this section as follows.</p>
<blockquote>
<p>3 If any declaration of a function has an <em>exception-specification</em>
<span class="raw-html"><span class="ins">that is not a <i>noexcept-specification</i> allowing all exceptions</span></span>,
all declarations, including the definition and an explicit
specialization, of that function shall have <span class="raw-html">a<span class="del">n</span> <span
class="ins">compatible</span> <i>exception-specification</i>
<span class="del">with the same set of <i>type-id</i>s</span>.</span> If
any declaration of a pointer to function, reference to function, or
pointer to member function has an <em>exception-specification</em>,
all occurrences of that declaration shall have <span class="raw-html">a<span class="del">n</span> <span
class="ins">compatible</span> <i>exception-specification</i>
<span class="del">with the same set of <i>type-id</i>s</span>.</span> In an
explicit instantiation an <em>exception-specification</em> may be
specified, but is not required. If an <em>exception-specification</em> is
specified in an explicit instantiation directive, it shall
<span class="raw-html"><span class="del">have the
same set of <i>type-id</i>s as</span><span class="ins">be compatible
with the <i>exception-specification</i>s of</span></span> other declarations
of that function.
A diagnostic is required only if the <span class="raw-html"><span class="del">sets of <i>type-id</i>s are
different</span><span class="ins"><i>exception-specifications</i>
are not compatible</span></span> within a single translation unit.</p>
<p><span class="ed">[Insert a new paragraph before paragraph 5]</span> <span class="raw-html"><span class="ins">Two
<i>exception-specifications</i> are <i>compatible</i> if:</span></span></p>
<blockquote>
<ul class="simple">
<li><span class="raw-html"><span class="ins">both are non-throwing (regardless of their form), </span></span></li>
<li><span class="raw-html"><span class="ins">both have the form <code>noexcept(<i>constant-expression</i>)</code> and the <i>constant-expression</i>s are equivalent,</span></span></li>
<li><span class="raw-html"><span class="ins">one <i>exception-specification</i> is a <i>noexcept-specification</i> allowing all exceptions and the other is of the form <code>throw(<i>type-id-list</i>)</code>, or</span></span></li>
<li><span class="raw-html"><span class="ins">both are <i>dynamic-exception-specifications</i> that have the same set of <i>type-id</i>s.</span></span></li>
</ul>
</blockquote>
<p>5 In such an assignment or initialization, <em>exception-specifications</em> on return types and parameter types shall <span class="del">match exactly</span> <span class="ins">be compatible</span>. In other assignments or initializations, <em>exception-specifications</em> shall <span class="del">match exactly</span> <span class="ins">be compatible</span>.</p>
<p><span class="ed">[Insert a new paragraph at the end of 15.4]</span> <span class="raw-html"><span class="ins">[ <i>Note</i>: The use of <i>dynamic-exception-specification</i>s is deprecated (see annex D). - <i>end note</i> ]</span></span></p>
</blockquote>
</div>
<div class="section" id="d-5-dynamic-exception-specifications-depr-except-spec">
<h2><a class="toc-backref" href="#id8"><span class="ins">D.5 Dynamic exception specifications [depr.except.spec]</span></a></h2>
<blockquote>
<span class="ins">1</span> <span class="raw-html"><span class="ins">The use of <i>dynamic-exception-specification</i>s is deprecated.</span></span></blockquote>
<hr class="docutils" />
<table class="docutils citation" frame="void" id="sutter02" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[Sutter02]</a></td><td>A Pragmatic Look at Exception Specifications. <a class="reference external" href="http://www.gotw.ca/publications/mill22.htm">http://www.gotw.ca/publications/mill22.htm</a></td></tr>
</tbody>
</table>
<table class="docutils citation" frame="void" id="boost03" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[Boost03]</a></td><td><a class="reference external" href="http://www.boost.org/development/requirements.html#Exception-specification">http://www.boost.org/development/requirements.html#Exception-specification</a></td></tr>
</tbody>
</table>
<table class="docutils citation" frame="void" id="n3050" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id3">[N3050]</a></td><td><ol class="first last upperalpha simple" start="4">
<li>Abrahams, R. Sharoni, and D. Gregor. <em>Allowing Move Constructors to Throw</em>. Document number N3050=10-0040, ISO C++ Committee Post-Pittsburgh Mailing, March, 2010.</li>
</ol>
</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
