Hi-
I'm wondering if anyone has any feedback or knowledge of the following
problem. I have searched the MSDN Knowledge Base and not found anything
relevant...
I have an enum similar to:
enum FruitType { apple, pear, peach, mango };
And a class FruitContainer that has a member variable of FruitType,
declared with a bit-width of 2:
FruitType m_myFruit : 2;
and access functions:
FruitType GetMyFruit () { return m_myFruit; }
void SetMyFruit(FruitType myFruit) { m_myFruit = myFruit; }
What we are seeing the Visual C++ 7.1 debugger is that if we call
SetMyFruit() with a value of apple or pear, and then retrieve the value
using GetMyFruit(), everything works as expected. But if we pass
SetMyFruit() the value mango, it ends up getting interpreted as the
integer value -1, and that is what is returned by GetMyFruit(). It
appears that the compiler or run-time is confused and considering the
enum value to be a signed quantity. The same code works correctly on
Solaris (compiled using Workshop) or Linux (using gcc). If I remove the
bit-width from the declaration, the code works correctly on Windows.
Also works if I set the bit-width to 3.
Thanks,
-Dennis McCrohan
Any biases, misguided opinions, or outright idiocy expressed in this
message are absolutely my own, and do not represent those of my
employer... 5 1625
Dennis McCrohan wrote: enum FruitType { apple, pear, peach, mango };
FruitType m_myFruit : 2;
The underlying type for an enumeration has to be an integral type that's
large enough to represent the values. There's no requirement that it be
unsigned if there are no negative values. Looks like the compiler is
using a signed type, which is allowed. So either change your bitfield
width to 3, declare the bitfield type as unsigned (which will require
some casts), or get rid of the bitfield entirely. That last one is
probably best: bitfields are best used for things like hardware access,
where bit positions matter.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Dennis McCrohan wrote: I'm wondering if anyone has any feedback or knowledge of the following problem. I have searched the MSDN Knowledge Base and not found anything relevant...
I have an enum similar to:
enum FruitType { apple, pear, peach, mango };
And a class FruitContainer that has a member variable of FruitType, declared with a bit-width of 2:
FruitType m_myFruit : 2;
and access functions:
FruitType GetMyFruit () { return m_myFruit; }
void SetMyFruit(FruitType myFruit) { m_myFruit = myFruit; }
What we are seeing the Visual C++ 7.1 debugger is that if we call SetMyFruit() with a value of apple or pear, and then retrieve the value using GetMyFruit(), everything works as expected. But if we pass SetMyFruit() the value mango, it ends up getting interpreted as the integer value -1, and that is what is returned by GetMyFruit(). It appears that the compiler or run-time is confused and considering the enum value to be a signed quantity. The same code works correctly on Solaris (compiled using Workshop) or Linux (using gcc). If I remove the bit-width from the declaration, the code works correctly on Windows. Also works if I set the bit-width to 3.
The underlying type for the 'enum FruitType' is apparently signed. You
have no control over that, I believe. The compiler picks the underlying
type for you (see 7.2/5). The compiler does not "get confused", it just
picks whatever it thinks is right.
If you want to make sure what type is used, do
typedef unsigned char FruitType;
FruitType const apple = 0, pear = 1, peach = 2, mango = 3;
....
Of course it has a side effect: the type is not unique and does not
differ from unsigned char, but at least you know it's unsigned...
Victor
Dennis McCrohan wrote: Hi-
I'm wondering if anyone has any feedback or knowledge of the following problem. I have searched the MSDN Knowledge Base and not found anything relevant...
I have an enum similar to:
enum FruitType { apple, pear, peach, mango };
And a class FruitContainer that has a member variable of FruitType, declared with a bit-width of 2:
Then enums are probably integers (not unsigned integers).
I'd guess if you make it of size 3, it would work.
But why use a bitfield here ?
Pete Becker <pe********@acm.org> wrote: bitfields are best used for things like hardware access, where bit positions matter.
How can you say that, given that in C++ the alignment and
allocation of bitfields is implementation-defined? In fact
I think they don't even have to be in the order that they
were declared.
(C99 is a bit stricter: the bits have to be in the order
declared and with no padding -- but it's still implementation-
defined whether the bits number left-to-right or right-to-left,
and what happens across unit boundaries)
Old Wolf wrote: Pete Becker <pe********@acm.org> wrote:
bitfields are best used for things like hardware access, where bit positions matter.
How can you say that, given that in C++ the alignment and allocation of bitfields is implementation-defined?
The compiler typically lines things up to match the hardware that it's
targeting.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: c duden |
last post by:
I am attempting to encrypt some text and be able to decrypt it at a later
time. I have two methods to do this:
public static Byte EncryptText(string textToEncrypt, string
encryptionHash)
{...
|
by: Steve |
last post by:
Hello,
I am using VSNET2003, in my solution there are some C# project and one
non_managed C++ project.
Suddently all my parameters in all my C# project are reset to nothing
in Visual Studio; So I...
|
by: Monique Chacon-Taylor |
last post by:
I have Visual Studio .Net 2003 version 7.1.3088 and .Net
Framework 1.1 version 1.1.4322.
I have a C# project. I can build it using the IDE fine,
but I cannot clean it. It leaves all files...
|
by: Veerle |
last post by:
Hi,
I have 5 files open in Visual Studio .NET. When I try to close the
second file, Visual Studio closes too. When I reopen Visual Studio and
then try to close the second file, Visual studio...
|
by: ORC |
last post by:
Applies to C#, Visual Studio 2003 version 7.1.3088.
I have a project where I have several linked files included. One problem is
that when I make changes in one of the linked file I will have to...
|
by: Brett |
last post by:
Hi,
I'm having trouble converting an old project to visual studio C++ .net
v7.1.3088
I've updated one of the header files to use #include <iostream>, and used
the std namspace.
After...
|
by: Brad |
last post by:
When debugging my current web project, in VS2003, I found I had lost the
ability to drill down on watch objects in the Watch Window; I could only
view the single value specific watch objects.
...
|
by: Fie Fie Niles |
last post by:
I have a WinXP Professional that had Microsoft Visual Studio .NET 2002. I
created a new project of type Visual Basic Projects and used
template: ASP.NET Web application. When I hit F5 to run the...
|
by: JonSteng |
last post by:
..Net Visual Studio Professional 2003 Version 7.1.3088
..Net Framework 1.1 SP1 Version 1.1.4322
IIS 5.1
Windows XP Professional SP2
Micron T3000 Laptop (1.5 GHz; 1GB RAM; 40GB HD with 17GB Free)...
|
by: mistral |
last post by:
Does Visual C++ 2005 Express Edition produce small, compact C
executables?
Or there is another C compilers that do this better?
m.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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...
|
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...
|
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...
| |