diff mbox

[wwwdocs] Add a note about in-class initialization of static data member

Message ID 20160211163958.GC3163@redhat.com
State New
Headers show

Commit Message

Marek Polacek Feb. 11, 2016, 4:39 p.m. UTC
On Thu, Feb 11, 2016 at 03:26:13PM +0000, Jonathan Wakely wrote:
> On 11/02/16 15:20 +0100, Marek Polacek wrote:
> >Does this look ok?
> 
> Looks OK, although how about stressing that it was only allowed as an
> extension previously, e.g. ...

So like this?  I've also added a note about stricter flexarr members rules.


	Marek

Comments

Jonathan Wakely Feb. 11, 2016, 5:01 p.m. UTC | #1
On 11/02/16 17:39 +0100, Marek Polacek wrote:
>On Thu, Feb 11, 2016 at 03:26:13PM +0000, Jonathan Wakely wrote:
>> On 11/02/16 15:20 +0100, Marek Polacek wrote:
>> >Does this look ok?
>>
>> Looks OK, although how about stressing that it was only allowed as an
>> extension previously, e.g. ...
>
>So like this?  I've also added a note about stricter flexarr members rules.

I think the first paragraph is a bit confusing. Both sentences are
true, but the first does not follow from the second, so "thus" is not
appropriate. Maybe just change "Thus" to "As a GNU extension".


>Index: porting_to.html
>===================================================================
>RCS file: /cvs/gcc/wwwdocs/htdocs/gcc-6/porting_to.html,v
>retrieving revision 1.9
>diff -u -r1.9 porting_to.html
>--- porting_to.html	10 Feb 2016 17:21:54 -0000	1.9
>+++ porting_to.html	11 Feb 2016 16:38:38 -0000
>@@ -269,6 +269,41 @@
> to port the code to use C++11's <code>std::unique_ptr</code> instead.
> </p>
>
>+<h3>'constexpr' needed for in-class initialization of static data member</h3>
>+
>+<p>
>+Since C++11, the <code>constexpr</code> keyword is needed when initializing
>+a non-integral static data member in a class.  Thus the following program is
>+accepted in C++03 (albeit with a <tt>-Wpedantic</tt> warning):
>+</p>
>+
>+<pre><code>
>+struct X {
>+  const static double i = 10;
>+};
>+</pre></code>
>+
>+<p>
>+The C++11 standard supports that in-class initialization using
>+<code>constexpr</code> instead, so the GNU extension is no longer supported for
>+C++11 or later.  Programs relying on the extension will be rejected with an
>+error.  The fix is to use <code>constexpr</code> instead of <code>const</code>.
>+</p>
>+
>+<h3>Stricter flexible array member rules</h3>
>+
>+<p>
>+As of this release, the C++ compiler is now more strict about flexible array
>+member rules.  As a consequence, the following code is no longer accepted:
>+</p>
>+
>+<pre><code>
>+union U {
>+  int i;
>+  char a[];
>+};
>+</pre></code>
>+
> <h2>-Wmisleading-indentation</h2>
> <p>
> A new warning <code>-Wmisleading-indentation</code> was added
>
>	Marek
Marek Polacek Feb. 11, 2016, 5:04 p.m. UTC | #2
On Thu, Feb 11, 2016 at 05:01:58PM +0000, Jonathan Wakely wrote:
> On 11/02/16 17:39 +0100, Marek Polacek wrote:
> >On Thu, Feb 11, 2016 at 03:26:13PM +0000, Jonathan Wakely wrote:
> >>On 11/02/16 15:20 +0100, Marek Polacek wrote:
> >>>Does this look ok?
> >>
> >>Looks OK, although how about stressing that it was only allowed as an
> >>extension previously, e.g. ...
> >
> >So like this?  I've also added a note about stricter flexarr members rules.
> 
> I think the first paragraph is a bit confusing. Both sentences are
> true, but the first does not follow from the second, so "thus" is not
> appropriate. Maybe just change "Thus" to "As a GNU extension".

Indeed, how could I not notice?  I'll go with "As a GNU extension".

Thanks much.

	Marek
Martin Sebor Feb. 11, 2016, 5:09 p.m. UTC | #3
On 02/11/2016 09:39 AM, Marek Polacek wrote:
> On Thu, Feb 11, 2016 at 03:26:13PM +0000, Jonathan Wakely wrote:
>> On 11/02/16 15:20 +0100, Marek Polacek wrote:
>>> Does this look ok?
>>
>> Looks OK, although how about stressing that it was only allowed as an
>> extension previously, e.g. ...
>
> So like this?  I've also added a note about stricter flexarr members rules.
>
> Index: porting_to.html
> ===================================================================
> RCS file: /cvs/gcc/wwwdocs/htdocs/gcc-6/porting_to.html,v
> retrieving revision 1.9
> diff -u -r1.9 porting_to.html
> --- porting_to.html	10 Feb 2016 17:21:54 -0000	1.9
> +++ porting_to.html	11 Feb 2016 16:38:38 -0000
> @@ -269,6 +269,41 @@
>   to port the code to use C++11's <code>std::unique_ptr</code> instead.
>   </p>
>
> +<h3>'constexpr' needed for in-class initialization of static data member</h3>
> +
> +<p>
> +Since C++11, the <code>constexpr</code> keyword is needed when initializing
> +a non-integral static data member in a class.  Thus the following program is
> +accepted in C++03 (albeit with a <tt>-Wpedantic</tt> warning):
> +</p>
> +
> +<pre><code>
> +struct X {
> +  const static double i = 10;
> +};

It''s interesting that when the example is modified to use a double
initializer it is rejected with a hard error even in C++ 03 mode
when -Wpedantic (but not -Werror) is used.  That seems like a bug.
If it isn't, it might be worth mentioning the constraint that the
initializer must be a integer in the text above.

   struct X {
     const static double i = 3.14;
   };

   error: floating-point literal cannot appear in a constant-expression
     const static double i = 3.14;
                            ^~~~

> +</pre></code>
> +
> +<p>
> +The C++11 standard supports that in-class initialization using
> +<code>constexpr</code> instead, so the GNU extension is no longer supported for
> +C++11 or later.  Programs relying on the extension will be rejected with an
> +error.  The fix is to use <code>constexpr</code> instead of <code>const</code>.
> +</p>
> +
> +<h3>Stricter flexible array member rules</h3>
> +
> +<p>
> +As of this release, the C++ compiler is now more strict about flexible array
> +member rules.  As a consequence, the following code is no longer accepted:

In light of bug 69550 I think it might be useful to also mention
(or show an example) that structs with a flexible array as the
only member are rejected as well.

Martin

> +</p>
> +
> +<pre><code>
> +union U {
> +  int i;
> +  char a[];
> +};
> +</pre></code>
> +
>   <h2>-Wmisleading-indentation</h2>
>   <p>
>   A new warning <code>-Wmisleading-indentation</code> was added
>
> 	Marek
>
diff mbox

Patch

Index: porting_to.html
===================================================================
RCS file: /cvs/gcc/wwwdocs/htdocs/gcc-6/porting_to.html,v
retrieving revision 1.9
diff -u -r1.9 porting_to.html
--- porting_to.html	10 Feb 2016 17:21:54 -0000	1.9
+++ porting_to.html	11 Feb 2016 16:38:38 -0000
@@ -269,6 +269,41 @@ 
 to port the code to use C++11's <code>std::unique_ptr</code> instead.
 </p>
 
+<h3>'constexpr' needed for in-class initialization of static data member</h3>
+
+<p>
+Since C++11, the <code>constexpr</code> keyword is needed when initializing
+a non-integral static data member in a class.  Thus the following program is
+accepted in C++03 (albeit with a <tt>-Wpedantic</tt> warning):
+</p>
+
+<pre><code>
+struct X {
+  const static double i = 10;
+};
+</pre></code>
+
+<p>
+The C++11 standard supports that in-class initialization using
+<code>constexpr</code> instead, so the GNU extension is no longer supported for
+C++11 or later.  Programs relying on the extension will be rejected with an
+error.  The fix is to use <code>constexpr</code> instead of <code>const</code>.
+</p>
+
+<h3>Stricter flexible array member rules</h3>
+
+<p>
+As of this release, the C++ compiler is now more strict about flexible array
+member rules.  As a consequence, the following code is no longer accepted:
+</p>
+
+<pre><code>
+union U {
+  int i;
+  char a[];
+};
+</pre></code>
+
 <h2>-Wmisleading-indentation</h2>
 <p>
 A new warning <code>-Wmisleading-indentation</code> was added