 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Le Chaud Lapin Guest
|
Posted: Fri Apr 15, 2005 7:27 am Post subject: Using Enums As Cheap Namespaces |
|
|
I'm probably going to get castigated for my ignorance, but here goes:
I just used Visual Studio 7.0 (what is this thing really called anyway)
to compile a large project that had been compiling "flawlessly" under
Visual Studio 6.0. With the new compiler, I have no less than 700
errors, and they all seem to be related to my improper use of enums as
cheap namespaces. In a .hpp file I write:
enum Service_Code {COPY, DELETE, SEARCH,...} ;
In a .cpp file I use COPY, qualifying it with Service_Code so that VS
6.0 will not complain:
pier << Service_Code::COPY;
VS 6.0 compiles this with no errors. VS 7.0 complains:
LibraryFile.cpp(117) : error C2825:
'TDS::Waiter_File::Service_Code::COPY': cannot form a qualified name
LibraryFile.cpp(117) : error C2039: 'COPY' : is not a member of
'operator``global namespace'''
Could someone explain to me why there is a disparity, whether it should
or should not work, and what is the proper (portable and conforming)
way to achieve the same result with the same convenience?
Thanks. :)
-Le Chaud Lapin-
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Fri Apr 15, 2005 9:22 am Post subject: Re: Using Enums As Cheap Namespaces |
|
|
On 15 Apr 2005 03:27:16 -0400, Le Chaud Lapin
<unoriginal_username (AT) yahoo (DOT) com> wrote:
| Quote: | enum Service_Code {COPY, DELETE, SEARCH,...} ;
In a .cpp file I use COPY, qualifying it with Service_Code so that VS
6.0 will not complain:
pier << Service_Code::COPY;
|
As enum is a C legacy stuff, I'm pretty much sure they should not
introduce name scopes. Although in VC6 you access them like
Service_Code::COPY, COPY is still hanging in the enum's enclosing scope
accessible without Service_Code::.
--
Maxim Yegorushkin
[ 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
|
Posted: Fri Apr 15, 2005 9:26 am Post subject: Re: Using Enums As Cheap Namespaces |
|
|
"Le Chaud Lapin" <unoriginal_username (AT) yahoo (DOT) com> writes:
| Quote: | In a .hpp file I write:
enum Service_Code {COPY, DELETE, SEARCH,...} ;
In a .cpp file I use COPY, qualifying it with Service_Code so that VS
6.0 will not complain:
pier << Service_Code::COPY;
VS 6.0 compiles this with no errors. VS 7.0 complains:
LibraryFile.cpp(117) : error C2825:
'TDS::Waiter_File::Service_Code::COPY': cannot form a qualified name
LibraryFile.cpp(117) : error C2039: 'COPY' : is not a member of
'operator``global namespace'''
Could someone explain to me why there is a disparity, whether it should
or should not work, and what is the proper (portable and conforming)
way to achieve the same result with the same convenience?
|
It should not work. For VS 7.0, they seem to have fixed a bug Vs 6.0
had.
You can use
namespace Service_Code
{
enum
{
COPY,
DELETE,
SEARCH
};
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
ThosRTanner Guest
|
Posted: Fri Apr 15, 2005 9:26 am Post subject: Re: Using Enums As Cheap Namespaces |
|
|
The name inside an enum gets yanked up to the containing level, so when
you do
class Wibble
{
enum Fruit { Apple, Orange, Banana }
}
You have an enum Wibble::Fruit, and enumerations Wibble::Apple,
Wibble::Orange, Wibble::Banana.
This is for compatibilty with C.
VC6 was incorrect in allowing your code through. I'm not sure what the
best way to deal with your issue is, choices being:
1) To change the enum to a namespace containing the enum. That will
work only if you don't use the enum name.
2) Change the enum to a class containing the enum (as per wibble
above).
3) Wait for the extension to C++ to be approved which makes it possible
to make enums behave like you (and I!) want.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stefan Näwe Guest
|
Posted: Fri Apr 15, 2005 9:27 am Post subject: Re: Using Enums As Cheap Namespaces |
|
|
Le Chaud Lapin schrieb:
| Quote: | enum Service_Code {COPY, DELETE, SEARCH,...} ;
In a .cpp file I use COPY, qualifying it with Service_Code so that VS
6.0 will not complain:
pier << Service_Code::COPY;
VS 6.0 compiles this with no errors. VS 7.0 complains:
LibraryFile.cpp(117) : error C2825:
'TDS::Waiter_File::Service_Code::COPY': cannot form a qualified name
LibraryFile.cpp(117) : error C2039: 'COPY' : is not a member of
'operator``global namespace'''
Could someone explain to me why there is a disparity, whether it should
or should not work, and what is the proper (portable and conforming)
way to achieve the same result with the same convenience?
|
enums are not namespaces. You have to use the enum values unqualified:
pier << COPY;
or, if the enum is defined inside a class/namespace (as in your case, I
guess):
pier << TDS::Waiter_File::COPY;
HTH
Stefan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Isaac Rodriguez Guest
|
Posted: Sat Apr 16, 2005 12:06 am Post subject: Re: Using Enums As Cheap Namespaces |
|
|
| Quote: | As enum is a C legacy stuff, I'm pretty much sure they should not
introduce name scopes. Although in VC6 you access them like
Service_Code::COPY, COPY is still hanging in the enum's enclosing scope
accessible without Service_Code::.
|
Is there any effort on changing this for the next standard? It is
contra-intuitive the way it works now because of the syntax. The syntax is
similar to a struct, class, or namespace declaration, which they all
introduce a new scope, then you deal with enums, and they don't.
Also, enums do not really create a new type. There have been some instances
when I wished they did create a new type, so you can overload functions on
them or have some extra checking at compile time. I got a say that some
times it has been nice that they do not introduce a type, but I see some
type checking advantages if enums declared a new type that I think I could
live with its disadvantages.
Thanks,
- Isaac.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Markus Moll Guest
|
Posted: Sat Apr 16, 2005 5:44 pm Post subject: Re: Using Enums As Cheap Namespaces |
|
|
Isaac Rodriguez wrote:
| Quote: | As enum is a C legacy stuff
|
Imho enums are an important part of the language.
(See Java < 1.5, which lacked enums... pain in the...)
| Quote: | The syntax is
similar to a struct, class, or namespace declaration, which they all
introduce a new scope, then you deal with enums, and they don't.
|
http://www.research.att.com/~bs/evol-issues.html
ES029, ES030
I'd really appreciate scoped enumerators, but I think we won't see them as
suggested in ES029, because it would break C code. (C compatibility _is_ an
annoying feature) Maybe we will get a combined explicit/scoped enumerator.
| Quote: | Also, enums do not really create a new type.
|
What do you mean?
Is your compiler broken?
"[3.9.2 Compound types]
[...]
- enumerations, which comprise a set of named constant values. Each distinct
enumeration constitutes a different enumerated type, 7.2;"
and
"[7.2.4]
Each enumeration defines a type that is different from all other types.
[...]"
Markus
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Sat Apr 16, 2005 5:48 pm Post subject: Re: Using Enums As Cheap Namespaces |
|
|
Isaac Rodriguez wrote:
| Quote: | Also, enums do not really create a new type. There have been some instances
when I wished they did create a new type, so you can overload functions on
them or have some extra checking at compile time. I got a say that some
times it has been nice that they do not introduce a type, but I see some
type checking advantages if enums declared a new type that I think I could
live with its disadvantages.
|
They do create a new type; you can overload functions on them or have some
extra checking at compile time.
enum RGB {R, G, B};
enum CMY {C, M, Y};
void f(RGB x) { std::clog << "f(RGB " << x << ")n"; }
void f(CMY x) { std::clog << "f(CMY " << x << ")n"; }
int main()
{
RGB rgb = R;
CMY cmy = C;
f(rgb);
f(cmy); // They do overload
f(0); // ERROR: No implicit conversion
f(static_cast
}
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Sun Apr 17, 2005 2:23 pm Post subject: Re: Using Enums As Cheap Namespaces |
|
|
On 15 Apr 2005 20:06:09 -0400, Isaac Rodriguez
<discussion.support (AT) autodesk (DOT) com> wrote:
[]
| Quote: | Also, enums do not really create a new type. There have been some
instances
when I wished they did create a new type, so you can overload functions
on
them or have some extra checking at compile time. I got a say that some
times it has been nice that they do not introduce a type, but I see some
type checking advantages if enums declared a new type that I think I
could live with its disadvantages.
|
Enums do introduce new types and you can overload operators and functions
for them.
enum E { one, two };
E operator++(E);
void f(int);
void f(E);
int main()
{
f(++one);
}
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|