473,756 Members | 5,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the range of enum

According to <<C++ Programming Language>>:
enum flag { x = 1, y = 2, z = 4, e = 8 }; //range 0:15
...
flag f4 = flag(99); // undefined : 99 is not within the range of flag


But the following codes has result 99.

//--------------------------------------------------
#include "iostream"

int main(int argc, char* argv[])
{
enum flag{ x = 1, y = 2, z = 4, e = 8 };

flag f1 = flag( 99 );

std::cout<<f1<< std::endl;

return 0;
}

//----------------------------------------------------
Whether the compiler treats enum type as integral type? Then what's the
meaning of the range of enum?
Jul 23 '05 #1
14 9992
Vane wrote:
According to <<C++ Programming Language>>:
enum flag { x = 1, y = 2, z = 4, e = 8 }; //range 0:15
...
flag f4 = flag(99); // undefined : 99 is not within the range of flag


But the following codes has result 99.


"Undefined" means "the result can be anything". And this includes a
value of 99.

Jul 23 '05 #2
This is an appropriate answer, as some compilers are implemented.
However, the compiler should have produced error at:

flag f4 = flag(99);

At this point, saying undefined means anything you do is OK, does not
seem justifiable.

Regards,
Dr. Z.
Chief Scientist
zo****@ZHMicro. com
http://www.zhmicro.com
http://distributed-software.blogspot.com

Jul 23 '05 #3
Zorro wrote:
This is an appropriate answer, as some compilers are implemented.
However, the compiler should have produced error at:

flag f4 = flag(99);


No, it shouldn't. This is a cast, and casting an integer into an enum is
allowed. It would be different if it were:

flag f4 = 99;

Jul 23 '05 #4
Well, it is a (constructor) cast. Let me try to expand on this a
little.

In actuality the compiler optimizes "flag f4 = flag(99)" and directly
initializes f4 with 99. Let us leave out optimization.

Then, "flag(99)" results in a temporary, call it T, which is
initialized with 99. At this point we have an error because the
compiler knows 99 is not a value of the type. Otherwise what is the use
of listing values for an enum?

At any rate, next the temporary T is assigned to f4 which is fine since
both are of the same type.

Now, you agree that "flag f4 = 99" is an error. Then how is the
creation of T permissible?

Regards,
Z.

Jul 23 '05 #5
Zorro wrote:
Well, it is a (constructor) cast. Let me try to expand on this a
little.
A "constructo r cast" does not exist.
In actuality the compiler optimizes "flag f4 = flag(99)" and directly
initializes f4 with 99.
Who said that?
Let us leave out optimization.
Good.
Then, "flag(99)" results in a temporary, call it T, which is
initialized with 99. At this point we have an error because the
compiler knows 99 is not a value of the type. Otherwise what is the use
of listing values for an enum?

At any rate, next the temporary T is assigned to f4 which is fine since
both are of the same type.

Now, you agree that "flag f4 = 99" is an error. Then how is the
creation of T permissible?


Because the language says it's undefined behavior, not an illegal
construct.
Jonathan

Jul 23 '05 #6
> Because the language says it's undefined behavior, not an illegal
construct.


That is all I am replying to.

Consider a switch statement that uses an enum value out of range, or
one that does not use them all. In these cases you will get a warning.
So the compiler does know about the range.

Now consider declaring something where the initializer is not of the
right type. Then you will get an error.

Now, in our case the initializer is not of the right type because the
value is not in the list. Just as the compiler can check that for a
switch statement, it can check it here too.

If that is the standard, someone will probably tell us. All I am saying
is that, according to general perception of C++ being strongly typed,
and the availability of information at the point of declaration, that
is an error. If it were impossible to determine, then the term
undefined would be appropriate.

Nevertheless, if the language identifies this error as undefined, so be
it.

Of course the statement is not illegal. But are all (syntactically)
legal statements permissible?

Regards,
Z.

Jul 23 '05 #7
Zorro wrote:
Because the language says it's undefined behavior, not an illegal
construct.
That is all I am replying to.

Consider a switch statement that uses an enum value out of range, or
one that does not use them all. In these cases you will get a warning.
So the compiler does know about the range.


Yes.
Now consider declaring something where the initializer is not of the
right type. Then you will get an error.
If there is no suitable conversion, yes.
Now, in our case the initializer is not of the right type because the
value is not in the list.
Right, that's why you get an error when writing:

flag f4 = 99;
Just as the compiler can check that for a switch statement, it can check
it here too.
Well, if you write:

flag f4 = flag(99);

you use a cast, which explicitly says to the compiler: "I know that it
doesn't fit, but I know what I'm doing, so shut up and do it anyway."
This transfers the responsibility to you.

The bottom line: Use a cast only if you are really sure you know it's the
right thing to do.
If that is the standard, someone will probably tell us. All I am saying
is that, according to general perception of C++ being strongly typed,
and the availability of information at the point of declaration, that
is an error.
With the "right" cast, you can do almost any conversion you like, even those
that might not make sense.
If it were impossible to determine, then the term undefined would be
appropriate.


It is possible, but a cast was used to explicitly switch that determination
off.

Jul 23 '05 #8
This is very well put. I have a different opinion, not necessarily
better than yours.

The notation flag(99) is indeed referred to as cast (copied from ADA).
However, in C++ that is also a constructor and is expected to produce
an instance of its type. In ADA something like int(x) returns a literal
value, not an object.

In C++, I think, "int i = int(5);" is the same as:

My_class_type X = My_class_type(a , b, c);

which is equivalent to:

My_class_type X(a, b, c);

However, the semantics of the first version (in my opinion) is that the
right hand side is creating a temporary instance, just like when we use
it as argument to a call, like this:

some_function(M y_class_type(a, b, c));

Several compilers will generate error if the pass is by reference
because the instance being created for the argument is a temporary (GNU
and Metrowerks do that).

So, perhaps the notation flag(99) has two different meanings in C++.

Regards,
Z.

Jul 23 '05 #9
Zorro wrote:
This is very well put. I have a different opinion, not necessarily
better than yours.
Please, quote the message you are answering to.
The notation flag(99) is indeed referred to as cast (copied from ADA).
This is not a cast.

flag f = static_cast<fla g>(99); // cast
flag f = flag(99); // copy-construction
However, in C++ that is also a constructor and is expected to produce
an instance of its type. In ADA something like int(x) returns a literal
value, not an object.
What does ADA have to do with this?
In C++, I think, "int i = int(5);" is the same as:

My_class_type X = My_class_type(a , b, c);
If you mean that it calls constructor, yes.
which is equivalent to:

My_class_type X(a, b, c);
Not necessarily. The first may call the copy-constructor and the second
calls one of the constructors with arguements.
However, the semantics of the first version (in my opinion) is that the
right hand side is creating a temporary instance, just like when we use
it as argument to a call, like this:

some_function(M y_class_type(a, b, c));
Yes.
Several compilers will generate error if the pass is by reference
because the instance being created for the argument is a temporary (GNU
and Metrowerks do that).
It is illegal to bound a non-const reference to a rvalue, if that's
what you mean. All compilers should give an error.
So, perhaps the notation flag(99) has two different meanings in C++.


What meanings?
Jonathan

Jul 23 '05 #10

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

Similar topics

1
2658
by: Vinodh Kumar P | last post by:
I don't understand what is the range of an enumeration. -Vinodh
6
1632
by: LordHog | last post by:
Hello all, My lead wants to implement a data range monitor for a project that we are coding. Basically it performs a boundry checking that will take three parameters. I am/was trying to implement a generic data range monitor, but it doesn't quite work. I am trying to create one method that will accept any type of parameter and use those values whether they be int, unsigned int, float, double etc.... I am not sure if this can be done...
21
4600
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
3616
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;
13
12392
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
1
2460
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and installed the boost library. This is using gcc under FC3.
34
11198
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?
8
3211
by: Rahul | last post by:
Hello Everyone, I was wondering what is the datatype of obj? Is it an integer? If so what if the number of enumerations crosses over the limit of an integer (2^32-1)? enum { CLUB, DIAMONDS
4
3129
by: Daniel Pitts | last post by:
I have a template: template<typename c, unsigned size> struct Vector { c components; template<unsigned index> c &get() { return components; } }; Vector<double, 3vect;
0
9455
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
9271
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
10031
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
9708
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
5140
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
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
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
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
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.