473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How defined is this?

Problem: I want to pass an Enum as an integer reference.

Solution:

void MyFunc ( int& MyInt )
{
std::cout << "Value is:" << MyInt << std::endl;
}

enum Enumerator
{
ZERO = 0,
ONE,
TWO,
THREE
};

int main()
{
Enumerator MyEnum = ONE;

if ( sizeof( MyEnum ) == sizeof( int ) )
MyFunc( *reinterpret_cast<int*>( &MyEnum ) );
}

Is this guaranteed to work if sizeof( Enumerator ) == sizeof( int ) ?

By work I mean is this program guaranteed to output 1 in all cases?
Aug 18 '06 #1
4 1176
Jim Langston wrote:
Problem: I want to pass an Enum as an integer reference.

Solution:

void MyFunc ( int& MyInt )
{
std::cout << "Value is:" << MyInt << std::endl;
}
Why is it a non-const reference? You don't change MyInt within the function.
enum Enumerator
{
ZERO = 0,
ONE,
TWO,
THREE
};

int main()
{
Enumerator MyEnum = ONE;

if ( sizeof( MyEnum ) == sizeof( int ) )
MyFunc( *reinterpret_cast<int*>( &MyEnum ) );
}

Is this guaranteed to work if sizeof( Enumerator ) == sizeof( int ) ?
Well, the result of reinterpret_cast is always compiler-specific, so it's
not guaranteed, AFAICS. Better make the parameter const. Then you can leave
out the cast and you won't depend on the size of your enum. A conversion to
int is then done automatically and guaranteed to work correctly, as long as
all your enum values are in the range of int.
So you just make it:

void MyFunc (const int& MyInt )
{
std::cout << "Value is:" << MyInt << std::endl;
}

and

int main()
{
Enumerator MyEnum = ONE;
MyFunc(MyEnum);
}
Aug 18 '06 #2
Jim Langston posted:
Is this guaranteed to work if sizeof( Enumerator ) == sizeof( int ) ?
No. For all you know, the system might have the following setup:

int: 32 value bits, 0 padding bits.
short: 16 value bits, 16 padding bits.

A more exhaustive approach would be:

#include <imax_bits.h>

#define VALBITS(T) IMAX_BITS((T)-1)

STATIC_ASSERT(sizeof(Enumerator) == sizeof(int));

STATIC_ASSERT(VALBITS(Enumerator) == VALBITS(int));

This would be guaranteed to work for unsigned types. It would be guaranteed
to worked for signed types also, if we can be sure that every signed integer
type uses the same system for storing negative numbers.

For information on "IMAX_BITS", go to:

http://groups.google.ie/group/comp.l...4859148?hl=en&

--

Frederick Gotham
Aug 18 '06 #3
In article <Dm***************@newsfe02.lga>,
Jim Langston <ta*******@rocketmail.comwrote:
>Problem: I want to pass an Enum as an integer reference.

Solution:

void MyFunc ( int& MyInt )
{
std::cout << "Value is:" << MyInt << std::endl;
}

enum Enumerator
{
ZERO = 0,
ONE,
TWO,
THREE
};

int main()
{
Enumerator MyEnum = ONE;

if ( sizeof( MyEnum ) == sizeof( int ) )
MyFunc( *reinterpret_cast<int*>( &MyEnum ) );
}

Is this guaranteed to work if sizeof( Enumerator ) == sizeof( int ) ?
Not in C++.
>By work I mean is this program guaranteed to output 1 in all cases?
Not sure what you're doing or why... why doesn't MyFunc just accept
an Enumerator, or just an int for that matter?
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 18 '06 #4
"Greg Comeau" <co****@panix.comwrote in message
news:ec**********@panix2.panix.com...
In article <Dm***************@newsfe02.lga>,
Jim Langston <ta*******@rocketmail.comwrote:
>>Problem: I want to pass an Enum as an integer reference.

Solution:

void MyFunc ( int& MyInt )
{
std::cout << "Value is:" << MyInt << std::endl;
}

enum Enumerator
{
ZERO = 0,
ONE,
TWO,
THREE
};

int main()
{
Enumerator MyEnum = ONE;

if ( sizeof( MyEnum ) == sizeof( int ) )
MyFunc( *reinterpret_cast<int*>( &MyEnum ) );
}

Is this guaranteed to work if sizeof( Enumerator ) == sizeof( int ) ?

Not in C++.
>>By work I mean is this program guaranteed to output 1 in all cases?

Not sure what you're doing or why... why doesn't MyFunc just accept
an Enumerator, or just an int for that matter?
What I'm actually going to be using this for is to get the address of
MyEnum.

Which is why the function is not const int&, because if it is and someone
does
MyFunc( MyEnum);
the compiler will make a temporary int, load enum into it and pass that, and
taking the address of it gives me the wrong address.

Thinking about it though, the whole reason for this was so the user wouldn't
have to type
MyFunc( &MyEnum );

if I had
void MyFunc( const int* val );
but for enums I think it would be better for them to just pass the address
then this whole thing. I"ll just make a seperate function for that.

Thanks.
Aug 19 '06 #5

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

Similar topics

4
by: Dave | last post by:
Is the construct #if defined MACRO_NAME part of standard C++? If so, how does it differ from #ifdef? Thanks!
5
by: lovecreatesbeauty | last post by:
Do #ifdef or #ifndef have some defects? I ever heard that some people use #if defined() or #if !defined() instead of using #ifdef or #ifndef in header file.
2
by: David Emme | last post by:
Access 97 I have a number of SELECT statements which contain references to user-defined VBA functions. These typically work as expected, but occasionally, on one user's machine or another,...
3
by: Leiji Liu | last post by:
Hi, I am curious if there are any pre-defined varables (constants?) in C? I saw some code with __LINE__, __FILE__, etc. Are those located in some include files? LL
8
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
1
by: sethuganesh | last post by:
HI, i have ported vc++ 6.0 code to visual studio 2005. During batch build in debug mode i din't get any error.But if i build the same in release mode i am getting the following error. ...
2
by: curious2007 | last post by:
During the linking I get the following: 1>Linking... 1>main.obj : error LNK2005: "double __cdecl sigma(class curious2007::pair<double,double> const &)" (?sigma@@YANABV?$pair@NN@curious2007@@@Z)...
30
by: Ioannis Vranos | last post by:
AFAIK the following is implementation-defined behaviour, am I right?: #include <stdio.h> int main(void) { int n= 0;
1
by: Gavin Chen | last post by:
Hello: I tried to install Tk800.015 on SunOS 4.1.4 with perl 5.6.2. At "make test" time, I got the error message as below: collect2: ld returned 2 exit status ld:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
0
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...
0
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,...
0
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...

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.