diff mbox

[wwwdocs] Add some C++ bits to porting_to

Message ID 20160204123726.GD31562@redhat.com
State New
Headers show

Commit Message

Marek Polacek Feb. 4, 2016, 12:37 p.m. UTC
I spotted these two issues in real-world code when doing Fedora mass
rebuild.  It's C++ and so I don't really know what I'm talking about;
Jon, could you please look at this?

Thanks,


	Marek

Comments

Jonathan Wakely Feb. 4, 2016, 1:01 p.m. UTC | #1
On 04/02/16 13:37 +0100, Marek Polacek wrote:
>I spotted these two issues in real-world code when doing Fedora mass
>rebuild.  It's C++ and so I don't really know what I'm talking about;
>Jon, could you please look at this?


>Thanks,
>
>Index: porting_to.html
>===================================================================
>RCS file: /cvs/gcc/wwwdocs/htdocs/gcc-6/porting_to.html,v
>retrieving revision 1.3
>diff -u -r1.3 porting_to.html
>--- porting_to.html	2 Feb 2016 20:34:14 -0000	1.3
>+++ porting_to.html	4 Feb 2016 12:33:19 -0000
>@@ -107,6 +107,60 @@
>   bool valid(std::ostream& os) { return (bool)os; }
> </code></pre>
>
>+<h4>Lvalue required as left operand of assignment with complex numbers</h4>
>+
>+<p>
>+Since C++11 (as per DR#387) the member functions <code>real()</code> and
>+<code>imag()</code> of <code>std::complex</code> can no longer be used as
>+lvalues, thus the following code is rejected:
>+</p>
>+<pre><code>
>+  std::complex<double> f;
>+  f.real () = val;
>+</code></pre>
>+
>+<p>
>+To assign <code>val</code> to the real component of <code>f</code>, the
>+following should be used instead:
>+</p>
>+<pre><code>
>+  std::complex<double> f;
>+  f.real (val);
>+</code></pre>

Looks good.

>+<h4>Destructors are <code>noexcept</code> by default</h4>
>+
>+<p>
>+In C++11, destructors are implicitly declared as <code>noexcept</code>.  In
>+practice this means that the following program behaves differently in C++11
>+than in C++03:
>+</p>
>+<pre><code>
>+  #include &lt;stdexcept&gt;
>+  struct S
>+  {
>+    ~S() { throw std::runtime_error ("oops"); }
>+  };
>+  int
>+  main (void)
>+  {
>+    try { S s; }
>+    catch (...) {
>+      return 42;
>+    }
>+  }
>+</code></pre>
>+
>+<p>
>+While in C++03 this program returns 42, in C++11 it terminates with a call to
>+<code>std::terminate</code>.  A new warning, <code>-Wterminate</code>, will
>+warn about such code.  It is possible to restore the old behavior when
>+defining the destructor like this:
>+</p>
>+<pre><code>
>+    ~S() noexcept(false) { throw std::runtime_error ("oops"); }
>+</code></pre>
>+

After you mentioned this on IRC I wrote the following. I think yours
is probably a bit simpler to read and so better, but is it worth
taking the "unless a base class or member variable has a destructor
that is noexcept(false)" part from mine? The noexcept on destructors
is deduced from the destructors of bases and members, so it might be
implicitly noexcept(true) or implicitly noexcept(false).


<h4>Destructors are implicitly <code>noexcept</code></h4>

<p>
As of C++11, destructors have an implicit <code>noexcept</code>
exception-specification (unless a base class or non-static member variable
has a destructor that is <code>noexcept(false)</code>). This means that
an exception thrown from a destructor will call <code>std::terminate()</code>
to exit the program. TO prevent this, destructors that need to throw should
be given an explicit <code>noexcept(false)</code> exception-specification.
</p>

<p>
By default GCC will now issue a warning for throw-expressions in
<code>noexcept</code> functions, including destructors, that would immediately
result in a call to terminate. The new warning can be disabled with
<tt>-Wno-terminate</tt>.
</p>
diff mbox

Patch

Index: porting_to.html
===================================================================
RCS file: /cvs/gcc/wwwdocs/htdocs/gcc-6/porting_to.html,v
retrieving revision 1.3
diff -u -r1.3 porting_to.html
--- porting_to.html	2 Feb 2016 20:34:14 -0000	1.3
+++ porting_to.html	4 Feb 2016 12:33:19 -0000
@@ -107,6 +107,60 @@ 
   bool valid(std::ostream&amp; os) { return (bool)os; }
 </code></pre>
 
+<h4>Lvalue required as left operand of assignment with complex numbers</h4>
+
+<p>
+Since C++11 (as per DR#387) the member functions <code>real()</code> and 
+<code>imag()</code> of <code>std::complex</code> can no longer be used as
+lvalues, thus the following code is rejected:
+</p>
+<pre><code>
+  std::complex<double> f;
+  f.real () = val;
+</code></pre>
+
+<p>
+To assign <code>val</code> to the real component of <code>f</code>, the
+following should be used instead:
+</p>
+<pre><code>
+  std::complex<double> f;
+  f.real (val);
+</code></pre>
+
+<h4>Destructors are <code>noexcept</code> by default</h4>
+
+<p>
+In C++11, destructors are implicitly declared as <code>noexcept</code>.  In
+practice this means that the following program behaves differently in C++11
+than in C++03:
+</p>
+<pre><code>
+  #include &lt;stdexcept&gt;
+  struct S
+  {
+    ~S() { throw std::runtime_error ("oops"); }
+  };
+  int
+  main (void)
+  {
+    try { S s; }
+    catch (...) {
+      return 42;
+    }
+  }
+</code></pre>
+
+<p>
+While in C++03 this program returns 42, in C++11 it terminates with a call to
+<code>std::terminate</code>.  A new warning, <code>-Wterminate</code>, will
+warn about such code.  It is possible to restore the old behavior when
+defining the destructor like this:
+</p>
+<pre><code>
+    ~S() noexcept(false) { throw std::runtime_error ("oops"); }
+</code></pre>
+
 <h3>Header dependency changes</h3>
 
 <p>