enums clash

J

Jarmo Muukka

Hello all!

I have used C++ for many years and recently I have used C#. Now I am coding
in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;


If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of
'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'


How would you fix these problems, if you would want to write it in C# way? I
would like to use logical names. It's not always possible to know what other
enums there are defined or will be defined.

JMu
 
M

mlimber

Jarmo said:
Hello all!

I have used C++ for many years and recently I have used C#. Now I am coding
in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;


If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of
'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'


How would you fix these problems, if you would want to write it in C# way? I
would like to use logical names. It's not always possible to know what other
enums there are defined or will be defined.

JMu

Well, generally speaking you should write in the language you're using,
not the language you've used elsewhere. Anyway, you can something like
what you want like this:

namespace Answer { enum Enum { Wrong, Right }; }
namespace Lane { enum Enum { Right, Left }; }

Answer::Enum ans = Answer::Right;
Lane::Enum lane = Lane::Right;

Cheers! --M
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Jarmo said:
Hello all!

I have used C++ for many years and recently I have used C#. Now I am coding
in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;


If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of
'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'


How would you fix these problems, if you would want to write it in C# way? I
would like to use logical names. It's not always possible to know what other
enums there are defined or will be defined.

C++ enum values are part of the surrounding namespace.
What about something like this:

//----------------------
namespace Lane
{
enum t { Left, Right };
}

namespace Answer
{
enum t { Right, Wrong};
}

int main()
{
Lane::t lane = Lane::Right;
Answer::t ans = Answer::Right;

// lane = ans; Won't work!!

return 0;
}

//----------------------


HTH

Stefan
 
V

Victor Bazarov

Jarmo said:
I have used C++ for many years and recently I have used C#. Now I am coding
in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;


If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of
'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'


How would you fix these problems, if you would want to write it in C# way? I
would like to use logical names. It's not always possible to know what other
enums there are defined or will be defined.

Start with

struct Lane { enum value { Left, Right }; };
struct Answer { enum value { Right, Wrong }; };

What you don't immediately have here is conversion to 'int'. And your
syntax becomes somewhat more convoluted

Lane::value lane = Lane::Right;

But you definitely acquire type safety.

V
 
J

Jarmo Muukka

Victor Bazarov said:
Start with

struct Lane { enum value { Left, Right }; };
struct Answer { enum value { Right, Wrong }; };

What you don't immediately have here is conversion to 'int'. And your
syntax becomes somewhat more convoluted

Lane::value lane = Lane::Right;

But you definitely acquire type safety.

V

Thank you mlimber, Stefan and Victor. I already have tried namespaces and
classes as typename. In my test I named my enums as Value (as Victor did). I
thought that if I have forgotten something and there would be better way,
but it seems that there isn't, so here is the code that I will use as base
technique from now on:

struct Lane
{
enum value
{
Left,
Right
};
};

struct Answer
{
enum value
{
Right,
Wrong
};
};

int _tmain( int argc, _TCHAR* argv[] )
{
Lane::value lane = Lane::Right;
Answer::value answer = Answer::Right;

// lane = answer;

return 0;
}

I am not 100% convinced that will I use namespace or struct, but this is
just a minor change in the definition, if I decide otherwise.

Thanks,
JMu
 
J

Jonathan Mcdougall

Jarmo said:
Hello all!

I have used C++ for many years and recently I have used C#. Now I am coding
in C++ and I have a problem now.

In C# you can do enums like this:

enum Lane
{
Left,
Right
}

enum Answer
{
Right,
Wrong
}

Lane lane = Lane.Right;
Answer answer = Answer.Right;


If you try to do similar in C++, it will not compile:

enum Lane
{
Left,
Right
};

enum Answer
{
Right, // <- compiler says Right is already defined
Wrong
};

Lane lane = Lane::Right; // <- compiler says 'Right' is not a member of
'Lane'
Answer answer = Answer::Right; // <- compiler says 'Right' is not a
member of 'Answer'


How would you fix these problems, if you would want to write it in C# way? I
would like to use logical names. It's not always possible to know what other
enums there are defined or will be defined.

# define scoped_enum(name) \
\
class name \
{ \
public: \
enum E \
{

# define scoped_enum_end(name) \
\
}; \
private: \
E e_; \
\
public: \
name() \
{ \
} \
\
name(E e) \
:e_(e) \
{ \
} \
\
name(int i) \
:e_(E(i)) \
{ \
} \
\
name operator|=(name t2) \
{ \
e_ = E( e_ | t2.e_ ); \
return *this; \
} \
\
name operator&=(name t2) \
{ \
e_ = E( e_ & t2.e_ ); \
return *this; \
} \
\
name operator^=(name t2) \
{ \
e_ = E( e_ ^ t2.e_ ); \
return *this; \
} \
\
operator int() \
{ \
return e_; \
} \
\
public: \
\
\
\
};

scoped_enum(Test)
one, two
scoped_enum_end(Test)

int main()
{
Test t1(Test::eek:ne);
Test t2(t1);

t1 |= Test::eek:ne;

if ( t1 & Test::eek:ne )
;

switch (t1)
{
case Test::eek:ne:
{
break;
}
}

if (t1 == Test::eek:ne)
;
}

It works pretty well, I think.


Jonathan
 
J

John Harrison

I am not 100% convinced that will I use namespace or struct, but this is
just a minor change in the definition, if I decide otherwise.

Not really. The main difference here between namespace and struct is
that you can add to a namespace later on, whereas a struct can only be
created once. So

namespace Lane
{
enum { Left, Right };
}

namespace Lane
{
enum { Inner, Outer };
}

is perfectly legal, but the same code with struct would not be. I
recommend that you use namespace.

john
 
A

Alf P. Steinbach

* John Harrison:
Not really. The main difference here between namespace and struct is
that you can add to a namespace later on, whereas a struct can only be
created once. So

namespace Lane
{
enum { Left, Right };
}

namespace Lane
{
enum { Inner, Outer };
}

is perfectly legal, but the same code with struct would not be. I
recommend that you use namespace.

Well, I'd consider that a reason why one should _not_ use a namespace
for wrapping an enum. ;-)

The main difference between class and namespace is, however, that a
class is a type that can be handled by template code.

E.g., it can be passed to enum-agnostic template code that infers min
and max values and whether a given template supports the ++ operator.
 
J

Jarmo Muukka

Jonathan Mcdougall said:
# define scoped_enum(name) \
\
class name \
{ \
public: \
enum E \
{

# define scoped_enum_end(name) \
\
}; \
private: \
E e_; \
\
public: \
name() \
{ \
} \
\
name(E e) \
:e_(e) \
{ \
} \
\
name(int i) \
:e_(E(i)) \
{ \
} \
\
name operator|=(name t2) \
{ \
e_ = E( e_ | t2.e_ ); \
return *this; \
} \
\
name operator&=(name t2) \
{ \
e_ = E( e_ & t2.e_ ); \
return *this; \
} \
\
name operator^=(name t2) \
{ \
e_ = E( e_ ^ t2.e_ ); \
return *this; \
} \
\
operator int() \
{ \
return e_; \
} \
\
public: \
\
\
\
};

scoped_enum(Test)
one, two
scoped_enum_end(Test)

int main()
{
Test t1(Test::eek:ne);
Test t2(t1);

t1 |= Test::eek:ne;

if ( t1 & Test::eek:ne )
;

switch (t1)
{
case Test::eek:ne:
{
break;
}
}

if (t1 == Test::eek:ne)
;
}

It works pretty well, I think.


Jonathan

Hello Jonathan,

Thanks for the tip. I modified my code to look like following (the actual
implementation has other types of course):

class Answer
{
public:
enum Code
{
Left,
Right
};

Answer( Code code )
: value( code )
{
}
operator int() const
{
return this->value;
}
private:
Code value;
};

With this implementation I can write:
Answer answer = Answer::Right;
Lane lane = Lane::Right;

The enum is a bit long compared to standard enum, but its usage looks good
to me.

Thanks again,
Jarmo
 
J

Jarmo Muukka

Alf P. Steinbach said:
* John Harrison:

Well, I'd consider that a reason why one should _not_ use a namespace
for wrapping an enum. ;-)

The main difference between class and namespace is, however, that a
class is a type that can be handled by template code.

E.g., it can be passed to enum-agnostic template code that infers min
and max values and whether a given template supports the ++ operator.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Hi,

I have dropped the idea of using namespace for wrapping an enum. The enums I
try to wrap can be wrapped with a class and it works nicely and the code
looks logical to me. See my other answer.

Thanks,
Jarmo
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top