Topic: enumerators, arrays, and linkage


Author: bparker@mailbox.uq.edu.au (Brian Parker)
Date: 1997/11/12
Raw View
On 12 Nov 97 02:53:06 GMT, Tom Payne <thp@cs.ucr.edu> wrote:

>...
>The C standard (6.1.2.2) stipulates that identifiers with internal or
>external linkage denote objects or functions, while "identifiers with
>*no linkage* denote unique entities."  It is my understanding that an
>enumerator denotes neither an object or a function but rather a value.
>(For instance, N has no address.)

3.5[basic.link]/2 states that "A name is said to have linkage when it
might denote the same object, reference, function, type,  template,
namespace or *value* as a name introduced by a declaration in another
scope." ,and 3.5/4 states that "An enumerator belonging to an
enumeration with external linkage... [has external linkage]".

So I suppose that this is deliberate difference with C, and it is at
least inconsistent to specify that an enumerator without external
linkage has no linkage rather than internal linkage.

But on further reading, the original example in this thread instancing
a class template with an enumerator is, I think, allowed regardless of
linkage issues because it is a constant integral expression.

,Brian Parker.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/11/12
Raw View
bparker@mailbox.uq.edu.au (Brian Parker) writes:

>But on further reading, the original example in this thread instancing
>a class template with an enumerator is, I think, allowed regardless of
>linkage issues because it is a constant integral expression.

Why does that make the example legal?

(I think the example _should_ be legal, but I don't think the wording
in the draft says that.)

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1997/11/12
Raw View
Fergus Henderson wrote:
>
> bparker@mailbox.uq.edu.au (Brian Parker) writes:
>
> >But on further reading, the original example in this thread instancing
> >a class template with an enumerator is, I think, allowed regardless of
> >linkage issues because it is a constant integral expression.
>
> Why does that make the example legal?
>
> (I think the example _should_ be legal, but I don't think the wording
> in the draft says that.)



Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/11/12
Raw View
Christopher Eltschka <celtschk@physik.tu-muenchen.de> writes:

>Fergus Henderson wrote:
>>
>> bparker@mailbox.uq.edu.au (Brian Parker) writes:
>>
>> >But on further reading, the original example in this thread instancing
>> >a class template with an enumerator is, I think, allowed regardless of
>> >linkage issues because it is a constant integral expression.
>>
>> Why does that make the example legal?
>>
>From CD2, 8.1 [dcl.name], 1
>[...]
>                  direct-abstract-declaratoropt [ constant-expressionopt
>]
>[...]
>
>Now, there' no mention of linkage here. Indeed the only mention of
>linkage
>in the whole chapter 8 is in a note about the linkage of an array with
>cv-qualified name. Now, a numerator is a constant expression. So what
>should deny the usage here?

The text I originally quoted at the start of this thread:

 |   3.5  Program and linkage                                  [basic.link]
 |
 |   [...]
 |
 | 8 Names not covered by these rules have no linkage.  [...]
 |   A name with no linkage [...] shall not be
 |   used to declare an entity with linkage.

>BTW, 100 has no linkage as well. I *really* hope that doesn't make
>  int a[100];
>illegal! :-)

It doesn't, because `100' is not a name.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/11/12
Raw View
bparker@mailbox.uq.edu.au (Brian Parker) writes:

>fjh@mundook.cs.mu.OZ.AU (Fergus Henderson) wrote:
>
>>bparker@mailbox.uq.edu.au (Brian Parker) writes:
>>
>>>But on further reading, the original example in this thread instancing
>>>a class template with an enumerator is, I think, allowed regardless of
>>>linkage issues because it is a constant integral expression.
>>
>>Why does that make the example legal?
>
>14.3[temp.arg]/3 states that a non-type template argument shall be an
>integral constant expression, amongst other things.
>
>But admittedly this contradicts 3.5/8, so I agree that the wording
>should probably be changed; in particular, the last sentence of 3.5/8
>should be changed to say "..template *type* arguments (14.3)." and  "A
>name with no linkage (notably..." should be changed to "A type name
>with no linkage (notably...".
>
>Or have I missed some subtlety of the linkage/ODR rules here?

Templates using non-type arguments that are not enumerators (e.g.
for pointer or reference parameters) should still be disallowed
if the argument has no linkage but the template does have linkage.
On the other hand, all those cases are explicitly disallowed
elsewhere, in 14.3 [temp.arg].  So I guess it would work for
templates.  But there are other cases not involving templates,
e.g.

 enum { Default = 42 };
 extern void f(int = Default);

 enum { Foo = 43 };
 struct Bar {
  int func() { return Foo; }
 };

These examples are currently ill-formed according to 3.5/8,
assuming that "used to declare" means "used in the declaration of",
but existing compilers accept them, and so they really ought to be allowed.

On a related note, there's also a problem with the wording of the ODR:

 |   3.2  One definition rule                               [basic.def.odr]
 |
 | 5 [...] Given such an entity named D defined in more than one
 |   translation unit, then
 |
 |   --each definition of D shall consist of the same sequence  of  tokens;
 |     and
 |
 |   --in each definition of D, corresponding names, looked up according to
 |     _basic.lookup_, shall refer to an entity defined within the  defini-
 |     tion of D, or shall refer to the same entity, after overload resolu-
 |     tion (_over.match_) and after matching of partial template  special-
 |     ization  (_temp.over_),  except  that  a  name  can refer to a const
 |     object with internal or no linkage if the object has the same  inte-
 |     gral  or enumeration type in all definitions of D, and the object is
 |     initialized with a constant expression (_expr.const_), and the value
 |     (but  not the address) of the object is used, and the object has the
 |     same value in all definitions of D; and

This paragraph ought to also allow names that refer to enumerators with
internal or no linkage, provided that the enumerator has the same
value in all definitions of D.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Tom Payne <thp@cs.ucr.edu>
Date: 1997/11/12
Raw View
In comp.std.c++ Brian Parker <bparker@mailbox.uq.edu.au> wrote:
: On 07 Nov 97 19:26:46 GMT, fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
: wrote:

: >The example is supposed to be at global scope.  `N' is an enumerator,
: >but it is not "an enumerator belonging to an enumeration with external
: >linkage" (3.5[basic.link]/4), so it doesn't have external linkage, and
: >hence (by 3.5/8) it has no linkage.
: >...

: Yes, that does seem to be a small omission in the draft standard. I
: would, of course, expect the enumerator N to have internal linkage if
: the enclosing enum does.

The C standard (6.1.2.2) stipulates that identifiers with internal or
external linkage denote objects or functions, while "identifiers with
*no linkage* denote unique entities."  It is my understanding that an
enumerator denotes neither an object or a function but rather a value.
(For instance, N has no address.)

Tom Payne
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: bparker@mailbox.uq.edu.au (Brian Parker)
Date: 1997/11/12
Raw View
On 12 Nov 97 09:33:06 GMT, fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
wrote:

>bparker@mailbox.uq.edu.au (Brian Parker) writes:
>
>>But on further reading, the original example in this thread instancing
>>a class template with an enumerator is, I think, allowed regardless of
>>linkage issues because it is a constant integral expression.
>
>Why does that make the example legal?

14.3[temp.arg]/3 states that a non-type template argument shall be an
integral constant expression, amongst other things.

But admittedly this contradicts 3.5/8, so I agree that the wording
should probably be changed; in particular, the last sentence of 3.5/8
should be changed to say "..template *type* arguments (14.3)." and  "A
name with no linkage (notably..." should be changed to "A type name
with no linkage (notably...".

Or have I missed some subtlety of the linkage/ODR rules here?

,Brian Parker.

---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/11/07
Raw View
"Paul D. DeRocco" <pderocco@ix.netcom.com> writes:

>Fergus Henderson wrote:
>>
>> 3.5[basic.link]/8 says "A name with no linkage ... shall not be used to
>> declare an entity with linkage".  Does that mean that the following
>> example is ill-formed?
>>
>>         enum { N = 100 };
>>         int a[N];
>
>The way I read it, N only has no linkage if it's declared in a
>local scope.

The example is supposed to be at global scope.  `N' is an enumerator,
but it is not "an enumerator belonging to an enumeration with external
linkage" (3.5[basic.link]/4), so it doesn't have external linkage, and
hence (by 3.5/8) it has no linkage.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: bparker@mailbox.uq.edu.au (Brian Parker)
Date: 1997/11/11
Raw View
On 07 Nov 97 19:26:46 GMT, fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
wrote:

>"Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
>
>>Fergus Henderson wrote:
>>>
>>> 3.5[basic.link]/8 says "A name with no linkage ... shall not be used to
>>> declare an entity with linkage".  Does that mean that the following
>>> example is ill-formed?
>>>
>>>         enum { N = 100 };
>>>         int a[N];
>>
>>The way I read it, N only has no linkage if it's declared in a
>>local scope.
>
>The example is supposed to be at global scope.  `N' is an enumerator,
>but it is not "an enumerator belonging to an enumeration with external
>linkage" (3.5[basic.link]/4), so it doesn't have external linkage, and
>hence (by 3.5/8) it has no linkage.
>...

Yes, that does seem to be a small omission in the draft standard. I
would, of course, expect the enumerator N to have internal linkage if
the enclosing enum does.

,Brian Parker.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/11/07
Raw View
Fergus Henderson wrote:
>
> 3.5[basic.link]/8 says "A name with no linkage ... shall not be used to
> declare an entity with linkage".  Does that mean that the following
> example is ill-formed?
>
>         enum { N = 100 };
>         int a[N];

The way I read it, N only has no linkage if it's declared in a
local scope. But if N is declared in a local scope, then a is
also, so it has no linkage either. Or if a is outside the local
scope, then it won't see N anyway.

--

Ciao,
Paul
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/11/05
Raw View
3.5[basic.link]/8 says "A name with no linkage ... shall not be used to
declare an entity with linkage".  Does that mean that the following
example is ill-formed?

 enum { N = 100 };
 int a[N];

Here `N' has no linkage, `N' is used in the declaration of `a',
and `a' has external linkage.

Similarly, how about this example?

 enum { N = 100 };
 template <int i> class C {};
 C<N> x;

3.5[basic.link/8] says "This implies that names with no linkage
cannot be used as template args.".

None of the compilers I tried reported any diagnostics for these
examples.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]