473,813 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare enum types to...

Is it possible to compare "enum" types to basic types like "unsigned short"
type at all? Thanks!

Jul 22 '05 #1
4 7038
slot wrote:
Is it possible to compare "enum" types to basic types like "unsigned short"
type at all? Thanks!


What do you mean by "compare types"? It's only possible in C++ to
compare _values_. And, if that's what you asked, then yes, it is
possible to compare values of some enum type to an unsigned short,
they will both be promoted to int (in most cases) and then compared.

Victor
Jul 22 '05 #2

"slot" <sl****@resort. com> wrote in message
news:Z_******** ************@bg tnsc05-news.ops.worldn et.att.net...
Is it possible to compare "enum" types to basic types like "unsigned short" type at all? Thanks!


Certainly! That's probaby more common than using a variable whose type is
actually an enum. (At least it is in my code.) A lot of code is written
like this:

// given an enum something like this:
enum { errNone, errNotFound, errRead, errWrite };

// code uses it like this:
int err = DoSomething();
if (errNone == err)
// continue with more stuff
else
// do something with the error value

-Howard
Jul 22 '05 #3
Howard wrote:
"slot" <sl****@resort. com> wrote in message
news:Z_******** ************@bg tnsc05-news.ops.worldn et.att.net...
Is it possible to compare "enum" types to basic types like "unsigned


short"
type at all? Thanks!

Certainly! That's probaby more common than using a variable whose type is
actually an enum. (At least it is in my code.) A lot of code is written
like this:

// given an enum something like this:
enum { errNone, errNotFound, errRead, errWrite };

// code uses it like this:
int err = DoSomething();
if (errNone == err)
// continue with more stuff
else
// do something with the error value


What is the advantage of your approach above versus

enum ErrCode { errNone, errNotFound, errRead, errWrite };
...
ErrCode err = DoSomething();
...

? Yes, your 'DoSomething' function now has to return ErrCode, otherwise
the initialisation of 'err' won't compile without an explicit cast, but
isn't that the whole point of type safety?

What if I just add 'errBadCmd' right between 'errNone' and 'errNotFound'?
The whole system suddenly stops working, doesn't it? Not in my case. If
ErrCode is used everywhere, then the module that contains 'DoSomething'
would have to be recompiled, but at least it will keep the values in sync.

Victor
Jul 22 '05 #4

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:yy******** *******@newsrea d1.dllstx09.us. to.verio.net...
Howard wrote:
"slot" <sl****@resort. com> wrote in message
news:Z_******** ************@bg tnsc05-news.ops.worldn et.att.net...
Is it possible to compare "enum" types to basic types like "unsigned


short"
type at all? Thanks!

Certainly! That's probaby more common than using a variable whose type is actually an enum. (At least it is in my code.) A lot of code is written like this:

// given an enum something like this:
enum { errNone, errNotFound, errRead, errWrite };

// code uses it like this:
int err = DoSomething();
if (errNone == err)
// continue with more stuff
else
// do something with the error value


What is the advantage of your approach above versus

enum ErrCode { errNone, errNotFound, errRead, errWrite };
...
ErrCode err = DoSomething();
...

? Yes, your 'DoSomething' function now has to return ErrCode, otherwise
the initialisation of 'err' won't compile without an explicit cast, but
isn't that the whole point of type safety?

What if I just add 'errBadCmd' right between 'errNone' and 'errNotFound'?
The whole system suddenly stops working, doesn't it? Not in my case. If
ErrCode is used everywhere, then the module that contains 'DoSomething'
would have to be recompiled, but at least it will keep the values in sync.

Victor


I never suggested my example was a "good" approach. I simply said integers
are compared against enum's quite often, and gave a legal, and simple,
example.

So why is "my" code filled with integers instead of enum's? It's because
the third-party APIs and SDKs declare their functions using intege types.
So I'm stuck using them. And most of my code is interacting with those, so
I'm pretty much stuck with it.

Also, many of those function calls use integers because the enumerations are
done by us, the developers of the end-user software, simply as a convenient
way to list, for example, IDs for all our controls in a music device. We're
free to use constants, enum's, or even literals, but enums are easier to
declare. SInce they (the API/SDK designers) don't care how we're going to
eventually define the values, they simply accept and return integers, which
have no restrictions on them (aside from range).

(Good points, though.)

-Howard



Jul 22 '05 #5

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

Similar topics

11
37105
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't explicitly say about enum's forward declaration, but one example shows it in 18.2.1, clause 4:
0
1746
by: Vaclav Haisman | last post by:
Motivation: I have been working on some project recently that uses lots of enums with disjunctive intervals of values because it is rather convenient way to define series of constants with consecutive values. The problem is that some functions should only accept some of the ranges. To store values of more than one range/enum in variable one has to use int as storage which imho compromises type safety because any function that wants to...
18
2388
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if there was a bit more typechecking ?
21
4611
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
3624
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
34
11210
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
12
6724
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: public enum Days { Sunday };
3
3047
by: hufaunder | last post by:
Imagine you have a charting library that can draw lines, bars, floating bars, bands, etc. Lines and bars need only one input. Floating bars and bands need two inputs. There are two approaches: 1) One enum with all 4 types (bars, band, etc). One chart class that accepts up to 2 arrays of values. If the user choses a band but there is only one input array throw an exception. If the user passes two input arrays with different lengths throw...
4
2702
by: jonpb | last post by:
The documentation for the "is" keyword says: An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown. So, given: enum TestEnum { t1, t2 } TestEnum te = TestEnum.t1;
0
9607
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
10407
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
10424
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
10140
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9224
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
7684
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
6897
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();...
0
5706
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4358
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

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.