How defined is this?

J

Jim Langston

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?
 
R

Rolf Magnus

Jim said:
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);
}
 
F

Frederick Gotham

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.lang.c/msg/1902a6f344859148?hl=en&
 
G

Greg Comeau

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?
 
J

Jim Langston

Greg Comeau said:
Not in C++.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top