473,722 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

resolving a warning error

I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.

Here is the sample code:

enum anEnum someFunc(void);

when i run:
xlc -c someFunc.c
i get
1506-103 (W) Tag anEnum requires a complete definition before it is
used.

Obviously, I could declare the enum, but
I would like to avoid declaring the enum as I would like
to hide the values of the enum from functions that don't need it.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition
of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?
Nov 14 '05 #1
11 3231
michael potter wrote:
I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.

Here is the sample code:

enum anEnum someFunc(void);

when i run:
xlc -c someFunc.c
i get
1506-103 (W) Tag anEnum requires a complete definition before it is
used.

Obviously, I could declare the enum, but
I would like to avoid declaring the enum as I would like
to hide the values of the enum from functions that don't need it.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition
of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


By using `int someFunc(void); ' instead.

"But I'll lose type safety!" Well, no. In the first
place, there's almost no type safety to begin with: C's
enums are pretty weak. In the second place, if you intend
to hide the enum's values from someFunc(), what will its
`return' statements look like? Where are you going to get
a known-to-be-valid value? Copy it from a global? Bah!

Or perhaps you intend to reveal the enum values to
someFunc() but hide them from someFunc()'s callers. What's
the point of that? It means that all the callers can do
with someFunc()'s returned value is pass it around among
themselves without understanding: They can't perform a
meaningful test with an `if' or a `switch' or any such;
all they can do is hand it from place to place as an
"opaque" value. An `int' is perfectly suited to such use.

`enum anEnum' doesn't seem to add value here.

--
Er*********@sun .com

Nov 14 '05 #2

In article <23************ **************@ posting.google. com>, po****@gmail.co m (michael potter) writes:
I would like to code a prototype that returns an enum, but not declare
the enum.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


You can't. Well, you may be able to do specifically what you ask
in your final question - eliminate one diagnostic from one particular
implementation - but that's off-topic here. What you cannot do is
use an enum without declaring it, in C. C permits incomplete structs
in various contexts; it does not permit incomplete enums. Some
implementations may allow the latter, but it is an extension.

There's a good reason for not allowing incomplete enums - the
implementation is allowed to pick any integral type for an enum,
provided that type can represent all the values of the enum. The
latter are always of type int, but they may fit in the range of, say,
unsigned char, in which case the implementation could use unsigned
char for that enum type.

If the implementation doesn't have a complete definition of the enum,
it may not know what integral type to use for it.

Of course, that doesn't preclude pointers to incomplete enums, just
as you can have pointers to incomplete structs. Why those were
omitted is a mystery to me; I suppose no one on the committee thought
they were sufficiently important.

--
Michael Wojcik mi************@ microfocus.com

Pocket #16: A Ventriloquist's "Helper" -- Recordings for Divers Occasions,
especially cries to put in the mouths of enemies -- "God Bless Captain
Vere!" "Les jeux sont faits!" &c. -- Joe Green
Nov 14 '05 #3
In <23************ **************@ posting.google. com> po****@gmail.co m (michael potter) writes:
I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.
Not if you invoke them in conforming mode:

fangorn:~/tmp 200> gcc test.c
fangorn:~/tmp 201> icc test.c
fangorn:~/tmp 202> gcc -ansi -pedantic test.c
test.c: In function `main':
test.c:8: warning: ISO C forbids forward references to `enum' types
fangorn:~/tmp 203> icc -Xc test.c
test.c(8): warning #102: forward declaration of enum type is nonstandard
enum anEnum someFunc(void);
^
The diagnostic is *required* by the C standard:

6.7.2.3 Tags

Constraints

1 A specific type shall have its content defined at most once.

2 A type specifier of the form

enum identifier

without an enumerator list shall only appear after the type it
specifies is complete.
How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


Your only chance is to find a way of invoking xlc in nonconforming mode,
because a conforming compiler MUST generate it, but you *really* don't
want to do that.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #4
In <cl**********@n ews1brm.Central .Sun.COM> Eric Sosman <er*********@su n.com> writes:
michael potter wrote:
I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.

Here is the sample code:

enum anEnum someFunc(void);

when i run:
xlc -c someFunc.c
i get
1506-103 (W) Tag anEnum requires a complete definition before it is
used.

Obviously, I could declare the enum, but
I would like to avoid declaring the enum as I would like
to hide the values of the enum from functions that don't need it.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition
of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


By using `int someFunc(void); ' instead.


Can I have the chapter and verse stating that enumerated types are
compatible with type int?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #5
Dan Pop wrote:
In <cl**********@n ews1brm.Central .Sun.COM> Eric Sosman <er*********@su n.com> writes:
michael potter wrote:
I would like to code a prototype that returns an enum, but not declare
the enum.
[...]
How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


By using `int someFunc(void); ' instead.


Can I have the chapter and verse stating that enumerated types are
compatible with type int?


No, of course not: 6.7.2.2/4 lists the allowable
types, and not all are necessarily compatible with `int'
on all implementations . However, any of the enumerated
values is compatible with `int' because each *is* an `int'.

It is, of course, possible to store a non-enumerated
value in an enum variable:

enum { FOO = 1, BAR = 3, BAZ = 5 } x = 2;

is legal C, unfortunately. (Note that if 2 were replaced
with 128 or -300 this would not be legal C: the compiler
might have chosen an integer type too narrow for these
non-enumerated values.) But I don't think that's a serious
objection -- after all, what the O.P. wants to do is already
a violation of C's rules, and substituting `int' seems a
reasonable way to begin a return to legality.

--
Er*********@sun .com

Nov 14 '05 #6
Da*****@cern.ch (Dan Pop) writes:
In <cl**********@n ews1brm.Central .Sun.COM> Eric Sosman
<er*********@su n.com> writes:
michael potter wrote: [...]
How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


By using `int someFunc(void); ' instead.


Can I have the chapter and verse stating that enumerated types are
compatible with type int?


They're assignment-compatible, which is good enough for the problem
under discussion.

Declaring

int someFunc(void);

in foo.h and

enum anEnum someFunc(void) { /* blah blah */ }

in foo.c could cause problems, particularly on systems where the types
have different sizes, but I don't think that's what Eric was
suggesting.

If you consistently treat someFunc as a function returning int, you
can still use enum anEnum internally.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
In <cl**********@n ews1brm.Central .Sun.COM> Eric Sosman <er*********@su n.com> writes:
Dan Pop wrote:
In <cl**********@n ews1brm.Central .Sun.COM> Eric Sosman <er*********@su n.com> writes:
michael potter wrote:

I would like to code a prototype that returns an enum, but not declare
the enum.
[...]
How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?

By using `int someFunc(void); ' instead.
Can I have the chapter and verse stating that enumerated types are
compatible with type int?


No, of course not: 6.7.2.2/4 lists the allowable
types, and not all are necessarily compatible with `int'
on all implementations .


Free clue: the concept of compatible types is NOT implementation
dependent. It is the C standard itself that specifies what types are
compatible:

1 Two types have compatible type if their types are the
same. Additional rules for determining whether two types are
compatible are described in 6.7.2 for type specifiers, in 6.7.3
for type qualifiers, and in 6.7.5 for declarators.
However, any of the enumerated
values is compatible with `int' because each *is* an `int'.


Which is entirely irrelevant here: if the function declaration doesn't
match the function definition, the behaviour is undefined:

2 All declarations that refer to the same object or function shall
have compatible type; otherwise, the behavior is undefined.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #8
In <cl**********@n ews1brm.Central .Sun.COM> Eric Sosman <er*********@su n.com> writes:
michael potter wrote:
I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.

Here is the sample code:

enum anEnum someFunc(void);

when i run:
xlc -c someFunc.c
i get
1506-103 (W) Tag anEnum requires a complete definition before it is
used.

Obviously, I could declare the enum, but
I would like to avoid declaring the enum as I would like
to hide the values of the enum from functions that don't need it.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition
of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?


By using `int someFunc(void); ' instead.

"But I'll lose type safety!" Well, no. In the first
place, there's almost no type safety to begin with: C's
enums are pretty weak. In the second place, if you intend
to hide the enum's values from someFunc(), what will its
`return' statements look like? Where are you going to get
a known-to-be-valid value? Copy it from a global? Bah!

Or perhaps you intend to reveal the enum values to
someFunc() but hide them from someFunc()'s callers.


Or neither. He may intend to hide the actual enum definition from
third party entities that neither define nor call someFunc(), they
merely pass its address to functions that have access to the actual
definition of enum anEnum. The logical equivalent of type FILE in
<stdio.h>.

It would be nice if that were possible, but it isn't. He must *define*
someFunc() as returning int, not only declare it as such.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #9
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern.c h (Dan Pop) writes:
In <cl**********@n ews1brm.Central .Sun.COM> Eric Sosman
<er*********@su n.com> writes:
michael potter wrote:[...] How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?

By using `int someFunc(void); ' instead.
Can I have the chapter and verse stating that enumerated types are
compatible with type int?


They're assignment-compatible, which is good enough for the problem
under discussion.


Maybe, but not for the suggested solution.
Declaring

int someFunc(void);

in foo.h and

enum anEnum someFunc(void) { /* blah blah */ }

in foo.c could cause problems, particularly on systems where the types
have different sizes,
It invokes undefined behaviour (unless enum anEnum happens to be
compatible with type int, but no program can make this assumption in
c.l.c):

2 All declarations that refer to the same object or function shall
have compatible type; otherwise, the behavior is undefined.

so I have no idea where you got your "could" and the nonsense about
different sizes from. But maybe you can provide some chapter and verse
that I have missed....
but I don't think that's what Eric was suggesting.
This is effectively what he wrote. He didn't suggest *any* change in the
function definition, did he?
If you consistently treat someFunc as a function returning int, you
can still use enum anEnum internally.


This is true, if by "consistent ly treat" you mean also defining it like
this.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #10

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

Similar topics

2
2600
by: Jean Brouwers | last post by:
Sporadically, a message like this one shows up in our wxPython application: (main.py:3182): Gtk-WARNING **: gtk_menu_attach_to_widget(): menu already attached to GtkMenuItem Is possible but tedious to find the cause of the message by inserting print statements. Is there an easier way, e.g. a traceback in Python when lower level messages occur?
7
1877
by: Charlie Brookhart | last post by:
I have a program (posted below) that is supposed to take liters, which is the user input, and convert it to pints and gallons. The pints and gallons are displayed in a read only textbox. I don't understand how to fix the problem. 'declaring variables Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) Dim totalPints As Double = Convert.ToDouble(txtPints.Text)
1
1251
by: olduncleamos | last post by:
Hi all, Just wondering if it is possible for a custom control to emit compiler warning or even error? It would be great to flag the user at compile time if they made some inconsistent property settings. Thanks in advance.
1
2341
by: Nathan Sokalski | last post by:
Visual Studio 2005 unexpectedly stopped generating the *.designer.vb files for *.aspx and *.ascx files. After a few days of frustration trying to fix this, I noticed that it had the following Warning in the Error List tab at the bottom of the screen: Generation of designer file failed: The relative virtual path '?/' is not allowed here. C:\Inetpub\wwwroot\CAT_aspnet\worldnews.aspx 0 0 CAT_aspnet
6
2270
by: David Mathog | last post by:
Do any of you happen to have links to compendiums (or heaven forbid, an actual manual) which maps compiler warnings and errors to examples of actual code problems? In this case I'm specifically looking for one for gcc. The general problem is of course that the compiler messages must be short, and that tends to make them so cryptic that it isn't always immediately obvious what the problem is. It almost makes me long for the days when the...
10
2333
by: MisterE | last post by:
typedef struct sg { int a; } G; int c(G* g) { return g->a; }
20
3251
by: jacob navia | last post by:
Consider this code static typedef struct { int boo; } FOO; This provokes with MSVC: ------------------------------ Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64 Copyright (C) Microsoft Corporation. All rights reserved.
3
3574
by: santhanalakshmi | last post by:
Hi, <?php mysql_connect('localhost','root',''); mysql_select_db('test'); $query="select * from phonedirectory"; $result=mysql_query($query); echo "One Way To Print"; echo "<br>";
0
8739
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
9384
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
9238
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...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8052
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...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2147
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.