🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

4 total messages Started by Aaron S. Binns Tue, 16 Apr 1996 20:17
'const' binding to specifier in declaration list
#2974
Author: Aaron S. Binns
Date: Tue, 16 Apr 1996 20:17
50 lines
2145 bytes
[Moderator's note: This is crossposted to comp.std.c++ and
comp.lang.c++, and followups have been set to comp.std.c++. mha]

I recently came across a piece of code that reads:

class A {
  // contents of class A
} a1, *a2, const *a3;


This looks pretty bogus to me.  I thought that the 'const' applied
to the elaborated specifier 'class A'.  If the 'const' qualifier is
supposed to apply to the pointer in 'a3', then it should come _after_
the '*'.

I think that either one of the two following was intended.

1)

class A {
  // contents of class A
} a1, *a2, * const a3;


2)

class A {
  // contents of class A
} a1, *a2;

A const *a3;  // Or const A *a3;


Is my interpretation of the binding of 'const' more or less correct?

BTW, this code _is_ accepted by the Microsoft VC/C++ v2.0 compiler.


-- 
 Aaron S. Binns 	                 Reliable Software Technologies Corp.
                        	         21515 Ridgetop Circle, Suite 250
 asbinn@rstcorp.com             	 Sterling, VA 20166
					http://www.rstcorp.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Re: 'const' binding to specifier in declaration list
#4087
Author: James Kanze US/ESC 60/3/141 #407...
Date: Wed, 17 Apr 1996 17:41
61 lines
2794 bytes
In article <3173B383.41C67EA6@rstcorp.com> "Aaron S. Binns"
<asbinn@rstcorp.com> writes:

|> [Moderator's note: This is crossposted to comp.std.c++ and
|> comp.lang.c++, and followups have been set to comp.std.c++. mha]

|> I recently came across a piece of code that reads:

|> class A {
|>   // contents of class A
|> } a1, *a2, const *a3;

While I personally wouldn't write anything like this, it is definitly
legal.

|> This looks pretty bogus to me.  I thought that the 'const' applied
|> to the elaborated specifier 'class A'.  If the 'const' qualifier is
|> supposed to apply to the pointer in 'a3', then it should come _after_
|> the '*'.

No, it is the A being pointed to that is const, not the pointer.

Separating each of the definitions out into a separate statement makes
this obvious:

    class A { ... } ;
    A               a1 ;
    A*              a2 ;
    A const*        a3 ;

|> I think that either one of the two following was intended.

|> 1)

|> class A {
|>   // contents of class A
|> } a1, *a2, * const a3;


|> 2)

|> class A {
|>   // contents of class A
|> } a1, *a2;

|> A const *a3;  // Or const A *a3;

Regardless of what was intended, 2 is the interpretation required by
the draft standard (and by the C standard).
-- 
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, itudes et rialisations en logiciel orienti objet --
                -- A la recherche d'une activiti dans une region francophone
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Re: 'const' binding to specifier in declaration list
#4088
Author: J. Stephen Adamczyk
Date: Wed, 17 Apr 1996 18:31
34 lines
2148 bytes
In article <KANZE.96Apr17121453@slsvgqt.lts.sel.alcatel.de> kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) writes:
>In article <3173B383.41C67EA6@rstcorp.com> "Aaron S. Binns"
><asbinn@rstcorp.com> writes:
>|> I recently came across a piece of code that reads:
>
>|> class A {
>|>   // contents of class A
>|> } a1, *a2, const *a3;
>
>While I personally wouldn't write anything like this, it is definitly
>legal.

No, I'm sorry, that's not true.  This little syntactic oddity is something
accepted by the Microsoft compiler, but it's not standard C or C++.
The part that gets repeated in a declaration comma list is the declarator,
and the cv-qualifiers can only appear following a pointer or pointer-to-member
declarator in the declarator syntax.

A little research with the Microsoft compiler suggests that:

  int i, const j, const *k;  // Declares j as "const int", k as "int *"

So the "const" above does nothing.  But that is outside the scope of
comp.std.c++.

Steve Adamczyk
Edison Design Group
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Re: 'const' binding to specifier in declaration list
#4101
Author: J. Kanze
Date: Thu, 18 Apr 1996 16:42
42 lines
2548 bytes
In article <KANZE.96Apr17121453@slsvgqt.lts.sel.alcatel.de>
kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) writes:

|> In article <3173B383.41C67EA6@rstcorp.com> "Aaron S. Binns"
|> <asbinn@rstcorp.com> writes:

|> |> [Moderator's note: This is crossposted to comp.std.c++ and
|> |> comp.lang.c++, and followups have been set to comp.std.c++. mha]

|> |> I recently came across a piece of code that reads:

|> |> class A {
|> |>   // contents of class A
|> |> } a1, *a2, const *a3;

|> While I personally wouldn't write anything like this, it is definitly
|> legal.

Actually, on further thought, I don't think it is legal.  In the statement:

	A const*		a3 ;

The const is part of the `declaration-specifiers', and not the
`declarator'.  When multiple symbols are declared in the same line, as
here, the list contains declarators, and cannot contain additional parts
of the declaration-specifiers.

A quick check of the grammar shows that the first token of a declarator
must be one of { `*', `(' , <identifier> }.  The keyword `const' is none
of these.
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
                            -- Beratung in industrieller Datenverarbeitung
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads