473,746 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the scope of enum?

I searched the C99 standard and clc's FAQ but was unable to
find an answer to the following issue. I hope that someone
here can illuminate me.

Suppose I have:

enum myenum { enuma, enumb, enumc };

near the top of a file, outside any functions.

Now, I don't want to have external linkage for this enum, that
is, I don't want it to be visible outside its translation unit.

So I added static to the declaration, as in:

static enum myenum { enuma, enumb, enumc };

When I compile this with gcc, it emits the message:

warning: useless keyword or type name in empty declaration

Therefore gcc does not like a static enum.

So I have the following questions:

1. Is an enumeration type, as described above, amenable
to external linkage?
2. If yes, then how does one hide a file-scope enum within
its translation unit?
3. If no, where in the standard is this behavior specified?

--
Rouben Rostamian
Nov 14 '05 #1
5 9379
ro****@pc18.mat h.umbc.edu (Rouben Rostamian) writes:
1. Is an enumeration type, as described above, amenable
to external linkage?
No. An enumeration type always has "no linkage".
2. If yes, then how does one hide a file-scope enum within
its translation unit?
If you're talking about an enumeration type, you need not take
any action, because an enumeration type has no linkage.

If you're talking about an object of enumeration type, you can
declare it `static'.
3. If no, where in the standard is this behavior specified?


See C99 6.2.2#6:

6 The following identifiers have no linkage: an identifier
declared to be anything other than an object or a function;
an identifier declared to be a function parameter; a block
scope identifier for an object declared without the
storage-class specifier extern.

An enumeration type is an identifier other than an object or a
function (it is a type).
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Nov 14 '05 #2
In article <87************ @benpfaff.org>,
Ben Pfaff <bl*@cs.stanfor d.edu> wrote:
See C99 6.2.2#6:

6 The following identifiers have no linkage: an identifier
declared to be anything other than an object or a function;
an identifier declared to be a function parameter; a block
scope identifier for an object declared without the
storage-class specifier extern.

An enumeration type is an identifier other than an object or a
function (it is a type).


Thanks for clarification and reference. This helps a lot.

--
Rouben Rostamian
Nov 14 '05 #3
Rouben Rostamian wrote:
.... snip ...
Suppose I have:

enum myenum { enuma, enumb, enumc };

near the top of a file, outside any functions.
.... snip ...
1. Is an enumeration type, as described above, amenable
to external linkage?
2. If yes, then how does one hide a file-scope enum within
its translation unit?
3. If no, where in the standard is this behavior specified?


I think the confusion arises because the "enum myenum ..."
statement is not declaring a variable, but a quasi-type, which is
identified by the complete string "enum myenum".

You could have followed this with:

enum myenum myvariable;

and myvariable could take on the enumerated values, be exported, or
marked static, etc.

I say quasi-type because C makes no attempts to restrict values
stored in myvariable to those enumerated.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #4
On Fri, 7 Jan 2005 20:36:12 +0000 (UTC), ro****@pc18.mat h.umbc.edu
(Rouben Rostamian) wrote:

[...]
When I compile this with gcc, it emits the message:

warning: useless keyword or type name in empty declaration

Therefore gcc does not like a static enum.

So I have the following questions:

1. Is an enumeration type, as described above, amenable
to external linkage?
2. If yes, then how does one hide a file-scope enum within
its translation unit?
3. If no, where in the standard is this behavior specified?


Since an enum is a pure structural declaration, it has usually no
memory location, because it doesn't need any memory. It is primarily
used for code clarity and increment-definition - the compiler cares
about that, but usually emits no memory for this.
So any storage type modifiers are ignored: No memory location, no
storage class.
--
,,,
_ _ \(((.
__,,../v\,----../ `-..=.>"" _\,_
_______;/____<_ \_______\ \___////______;__****** ***@pukys.de___ ____
,"/ `.) `.) ```
/," /7__ /7_
(( ' \\\ )))
)
/
Nov 14 '05 #5
Rouben Rostamian wrote:
Now, I don't want to have external linkage for this enum, that
is, I don't want it to be visible outside its translation unit.


One simple way of looking at it is that an enum is, in practice, much
like a sequence of #define statements:

enum myenum { enuma, enumb, enumc };

/* is about the same as */

#define enuma 0
#define enumb 1
#define enumc 2

You can see how the concept of linkage wouldn't apply, and how it does
in fact do what you want. Also see comp.lang.c FAQ question 2.22 at:

http://www.eskimo.com/~scs/C-faq/q2.22.html

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2483
by: Morgan Cheng | last post by:
Hi, In my program module, there are some Constants should be defined to be integer key value of std::map. In the module, methods of a few classes will return std::map containing value indexed by constant integer key value. I am wondering what is a good way to define these constants. 1) #define A = 0 #define B = 1 #define C = 2
2
6916
by: vsgdp | last post by:
Hi, Do enumerators have scope? class A { }; enum E {A, B, C}; int main() {
2
1419
by: Claire | last post by:
I have an enumeration of constants as follows. public enum eDeviceErrors { NoError = 0, ENOENT = 2, EBADF = 9, EACCES = 13, EINVAL = 22, Checksum = 40,
7
1449
by: moondaddy | last post by:
I want to create a public enum that can be used throughout a project. I created an enum like this in a module: Public Enum ParentType Project = 0 Stage = 1 VIP = 2 Func = 3 Equipment = 4 Idea = 5
2
1870
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product Public Enum Status psNormal psCharged End Enum
9
2547
by: Jacek Dziedzic | last post by:
Hi! I often find that my programs need to store information on "current mode of something" with two or at most several mutually exclusive "modes" to choose from, e.g. - datafile: is it in a) read-only mode or b) write-only mode, - a function picking points a) above, b) below or c) contained on a plane in 3D, etc.
1
4663
by: Joseph Turian | last post by:
I try to compile the following: === typedef enum { CREATED, ADDED_TO_AGENDA, POPPED_FROM_AGENDA, DUMMY_LAST_CAT } DStatCat; class DerivationStatistics { .... private: enum {MAX_LENGTH = 1000};
1
2149
by: wenmang | last post by:
Hi, I encountered some legacy codes with multiple definitions for some symbols in term of enum(global naemspace pollution). How can I enforce the scope of an enum? e.g., enum MyEnum { OK }; enum YourEnum {
11
1263
by: puzzlecracker | last post by:
Say we have the following class (adapted from the standard): enum {i=1}; class X{ char v; enum{i=2} // why this is illegal? int f(){ return sizeof(c); } // and this is legal? };
0
8975
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8801
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9516
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9351
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8229
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4587
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4840
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2200
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.