C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Forward declare enums

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Russ
Guest





PostPosted: Thu Jun 16, 2005 8:13 am    Post subject: Forward declare enums Reply with quote



Is there a portable way to forward declare enums?

It seems that some compilers (e.g. VC++) allow it, and others don't. I
need to fwd declare them but have it work with all compilers.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Me
Guest





PostPosted: Fri Jun 17, 2005 10:17 am    Post subject: Re: Forward declare enums Reply with quote



Quote:
Is there a portable way to forward declare enums?

No.

Quote:
It seems that some compilers (e.g. VC++) allow it, and others don't. I
need to fwd declare them but have it work with all compilers.

In C, "enum foo;" is only allowed after foo has been defined. In C++,
"enum foo;" isn't even allowed by the grammar. Compilers allow forward
enum declarations as a (very welcome) extension.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Hyman Rosen
Guest





PostPosted: Fri Jun 17, 2005 10:23 am    Post subject: Re: Forward declare enums Reply with quote



Russ wrote:
Quote:
Is there a portable way to forward declare enums?

No, it is not standard-conforming to do so.
There is also no need, since enums cannot take
part in mutually recursive data structures, which
is the only reason for forward declarations.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Thomas Maeder
Guest





PostPosted: Fri Jun 17, 2005 1:16 pm    Post subject: Re: Forward declare enums Reply with quote

"Russ" <cpp2319 (AT) hotmail (DOT) com> writes:


Quote:
Is there a portable way to forward declare enums?


No.



Quote:
It seems that some compilers (e.g. VC++) allow it, and others don't. I
need to fwd declare them but have it work with all compilers.


Why do you need to declare an enum without defining it?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ben Hutchings
Guest





PostPosted: Fri Jun 17, 2005 1:20 pm    Post subject: Re: Forward declare enums Reply with quote

Russ <cpp2319 (AT) hotmail (DOT) com> wrote:

Quote:
Is there a portable way to forward declare enums?

It seems that some compilers (e.g. VC++) allow it, and others don't. I
need to fwd declare them but have it work with all compilers.


No. The underlying integer type for an enumerated type can depend on
the values of its enumerators. The representation of a pointer or
reference to an enumerated type in turn can depend on that underlying
integer type. So the language does not allow for incomplete
enumerated types.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Torsten Robitzki
Guest





PostPosted: Fri Jun 17, 2005 1:36 pm    Post subject: Re: Forward declare enums Reply with quote

Russ wrote:
Quote:
Is there a portable way to forward declare enums?

It seems that some compilers (e.g. VC++) allow it, and others don't. I
need to fwd declare them but have it work with all compilers.

Then use an integer.

regards
Torsten


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dave Harris
Guest





PostPosted: Fri Jun 17, 2005 1:38 pm    Post subject: Re: Forward declare enums Reply with quote

[email]cpp2319 (AT) hotmail (DOT) com[/email] (Russ) wrote (abridged):
Quote:
Is there a portable way to forward declare enums?

No. You can wrap them in a class and forward-declare that.

struct Colour {
enum Type { red, green, blue };

Colour( Type v=red ) : value(v) {}
operator Colour() const { return value; }
private:
Type value;
};

-- Dave Harris, Nottingham, UK.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
cookie__raver@hotmail.com
Guest





PostPosted: Fri Jun 17, 2005 1:40 pm    Post subject: Re: Forward declare enums Reply with quote

Check out a recent column in Mathew Wilson's column in Dr Dobbs:
http://www.ddj.com/columns/wilson/


Russ wrote:
Quote:
Is there a portable way to forward declare enums?

It seems that some compilers (e.g. VC++) allow it, and others don't. I
need to fwd declare them but have it work with all compilers.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Russ
Guest





PostPosted: Sat Jun 18, 2005 9:19 am    Post subject: Re: Forward declare enums Reply with quote



Hyman Rosen wrote:
Quote:
Russ wrote:
Is there a portable way to forward declare enums?

No, it is not standard-conforming to do so.
There is also no need, since enums cannot take
part in mutually recursive data structures, which
is the only reason for forward declarations.

That's not so. Open your mind ... ;-)

I am using (compiler-specific, via VC++ 2003) it for a good reaon,
making library work for different programs in distributed sysstem with
different data type ids


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
danny1066@hotmail.com
Guest





PostPosted: Sat Jun 18, 2005 9:19 am    Post subject: Re: Forward declare enums Reply with quote

The STLSoft libraries provide a way that's portable, using a class
wrapper and macros, like:

// Forward declaration
STLSOFT_DECLARE_FWD_ENUM(MessageCode);


// Definition - can be diferent file
STLSOFT_DEFINE_FWD_ENUM_BEGIN(MessageCode)
mcEnter, mcExit, mcClear = 3, mcUnknown = -1, mcQuit = 4
STLSOFT_DEFINE_FWD_ENUM_END(MessageCode)


under the covers its all a real enum, but the class is the thing that's
forward declared, so it's entirely legal. You can even use it in a
switch statement like a normal enum! The STLSoft documetnation's pretty
bummed, as it is for all their libs, but the thing itself is really
cool. We use STLSofts libs for lots of things, and just notiecd this
had added in last release. One of my colleagues says Matt Wilsoin, who
writes STLSoft libraries, wrote about it in a recent CUJ online
aarticle, but havent seen it.


Download it at http://synesis.com.au/software/stlsoft/downloads.html

HTH

D.T.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gene Bushuyev
Guest





PostPosted: Sat Jun 18, 2005 9:32 am    Post subject: Re: Forward declare enums Reply with quote

<cookie__raver (AT) hotmail (DOT) com> wrote

Quote:
Check out a recent column in Mathew Wilson's column in Dr Dobbs:
http://www.ddj.com/columns/wilson/

Which is not a good article. Had the author wrapped enum into a struct he
wouldn't have too much to complain about:

struct EnumType; // lo, forward declaration! Not really useful, of course.

struct EnumType
{
enum type { first_const, second_const };
};

EnumType::type first_const() // look, no name clashing!
{
return EnumType::first_const;
}

Gene

P.S. author's namespace wrapping is not a solution, it's a namespace misuse.
P.P.S. author's using double underscores "__" and weird macros is telling
quite a bit about the quality of the solutions presented in the articles.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Hyman Rosen
Guest





PostPosted: Tue Jun 21, 2005 10:00 am    Post subject: Re: Forward declare enums Reply with quote

Russ wrote:
Quote:
That's not so. Open your mind ... ;-)

I am using (compiler-specific, via VC++ 2003) it for a good reaon,
making library work for different programs in distributed sysstem with
different data type ids

I'm just a simple programmer and your modern ways baffle and
confuse me, but I know that there is no need to forward declare
enums because you can just include the entire definition in the
same spot.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Llewelly
Guest





PostPosted: Tue Jun 21, 2005 6:30 pm    Post subject: Re: Forward declare enums Reply with quote

Hyman Rosen <hyrosen (AT) mail (DOT) com> writes:

Quote:
Russ wrote:
That's not so. Open your mind ... ;-)

I am using (compiler-specific, via VC++ 2003) it for a good reaon,
making library work for different programs in distributed sysstem with
different data type ids

I'm just a simple programmer and your modern ways baffle and
confuse me, but I know that there is no need to forward declare
enums because you can just include the entire definition in the
same spot.

Some people like to provide users of a lib with only the enum's type,
and keep the enumerators encapsulated where only the
implementation can see them.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Hyman Rosen
Guest





PostPosted: Wed Jun 22, 2005 8:02 am    Post subject: Re: Forward declare enums Reply with quote

Llewelly wrote:
Quote:
Some people like to provide users of a lib with only the enum's type,
and keep the enumerators encapsulated where only the
implementation can see them.

But that's not possible in C++, because you cannot forward declare an enum.

It's also impossible to "encapsulate" enum values, because they freely convert
to integers, and then all values between 0 and one less than the next higher
power of two of the converted value are legal to cast back to the enum type.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Francis Glassborow
Guest





PostPosted: Wed Jun 22, 2005 1:11 pm    Post subject: Re: Forward declare enums Reply with quote

In article <200506211840.j5LIe3Qd024100 (AT) cliffclavin (DOT) cs.rpi.edu>, Hyman
Rosen <hyrosen (AT) mail (DOT) com> writes
Quote:
Llewelly wrote:
Some people like to provide users of a lib with only the enum's type,
and keep the enumerators encapsulated where only the
implementation can see them.

But that's not possible in C++, because you cannot forward declare an enum.

It's also impossible to "encapsulate" enum values, because they freely convert
to integers, and then all values between 0 and one less than the next higher
power of two of the converted value are legal to cast back to the enum type.
And I still find it difficult to understand why programmers who want

(and often have good reasons for wanting) to 'forward' declare an enum
find difficulty with:

struct etype{
enum type{/* enumerators /*};
};

which can be declared with:

struct etype;

Even better, the enumerators are now properly scoped.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.