 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mark Guest
|
Posted: Fri Oct 29, 2004 1:50 am Post subject: Power of 2 enumerations |
|
|
Just curious,
Is there any proposal to add the above enumerations
to C++ in future?
For example, instead of
enum
{
FEATURE1 = 1,
FEATURE2 = 2,
FEATURE3 = 4
};
we could have
typedef binary enum
{
FEATURE1,
FEATURE2,
FEATURE3
}feature_t;
void foo( feature_t f )
{
if( f & FEATURE1 )
{
}
}
int main()
{
foo( FEATURE1 | FEATURE2 );
}
so the numbers are assigned automatically and there is
no casting required to OR the values in main, ie the
typing is retained.
Cheers,
Mark.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
assaarpa Guest
|
Posted: Fri Oct 29, 2004 5:20 pm Post subject: Re: Power of 2 enumerations |
|
|
| Quote: | Is there any proposal to add the above enumerations
to C++ in future?
|
I hope not, what you are proposing is something that can easily be done
using existing language constructs.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Fri Oct 29, 2004 6:28 pm Post subject: Re: Power of 2 enumerations |
|
|
"Mark" <user (AT) example (DOT) net> wrote
| Quote: | Just curious,
Is there any proposal to add the above enumerations
to C++ in future?
For example, instead of
enum
{
FEATURE1 = 1,
FEATURE2 = 2,
FEATURE3 = 4
};
we could have
typedef binary enum
{
FEATURE1,
FEATURE2,
FEATURE3
}feature_t;
|
But this is easy to do already:
enum features
{
FEATURE1 = 1,
FEATURE2 = FEATURE1 << 1,
FEATURE3 = FEATURE2 << 1
};
It think there's much more urgent need for this:
fibonacci enum features
{
FEATURE1,
FEATURE2,
FEATURE3,
FEATURE4
};
To replace the common but error-prone idiom:
enum features
{
FEATURE1 = 1,
FEATURE2 = 1,
FEATURE3 = FEATURE1 + FEATURE2,
FEATURE4 = FEATURE2 + FEATURE3,
...
};
;-)
Jonathan
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Falk Tannhäuser Guest
|
Posted: Fri Oct 29, 2004 10:04 pm Post subject: Re: Power of 2 enumerations |
|
|
Jonathan Turkanis wrote:
| Quote: | It think there's much more urgent need for this:
fibonacci enum features
{
FEATURE1,
FEATURE2,
FEATURE3,
FEATURE4
};
To replace the common but error-prone idiom:
enum features
{
FEATURE1 = 1,
FEATURE2 = 1,
FEATURE3 = FEATURE1 + FEATURE2,
FEATURE4 = FEATURE2 + FEATURE3,
...
};
|
Hey, that can already be done using existing language feature, too!
template<unsigned N> struct fibonacci
{ enum { feature = fibonacci<N-1>::feature + fibonacci<N-2>::feature }; };
template<> struct fibonacci<0> { enum { feature = 0 }; };
template<> struct fibonacci<1> { enum { feature = 1 }; };
| Quote: |
ditto... |
Falk
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Mark Guest
|
Posted: Sun Oct 31, 2004 8:11 am Post subject: Re: Power of 2 enumerations |
|
|
assaarpa wrote:
| Quote: | Is there any proposal to add the above enumerations
to C++ in future?
I hope not, what you are proposing is something that can easily be done
using existing language constructs.
|
The problem is it's not done very well at all with
current language constructs is it?
If you want to use this idiom, you have to make these
types integers or unsigned integers, so you loose the
type safety.
The other alternative is to use ugly casts to convert
the result back into your type. Neither is a great
solution!
So is there a more elegant solution I don't know about?
Cheers,
Mark.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
John Nagle Guest
|
Posted: Sun Oct 31, 2004 7:33 pm Post subject: Re: Power of 2 enumerations |
|
|
There are bit fields for that.
struct flags {
bool field1: 1;
bool field2: 1;
bool field3: 1;
};
That's the proper way to do bit fields. It's underutilized,
but works fine.
Unfortunately, bitfields and arrays don't interact well.
You can't create a packed array of bits:
struct bitmap {
bool pixel: 1 [640*480];
};
Logically, that should work, but it doesn't. Does anyone
know why?
John Nagle
Mark wrote:
| Quote: | assaarpa wrote:
Is there any proposal to add the above enumerations
to C++ in future?
I hope not, what you are proposing is something that can easily be
done using existing language constructs.
The problem is it's not done very well at all with
current language constructs is it?
If you want to use this idiom, you have to make these
types integers or unsigned integers, so you loose the
type safety.
The other alternative is to use ugly casts to convert
the result back into your type. Neither is a great
solution!
So is there a more elegant solution I don't know about?
Cheers,
Mark.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Mon Nov 01, 2004 2:52 am Post subject: Re: Power of 2 enumerations |
|
|
In article <eMahd.37057$QJ3.13078 (AT) newssvr21 (DOT) news.prodigy.com>, John
Nagle <nagle (AT) animats (DOT) com> writes
| Quote: | Unfortunately, bitfields and arrays don't interact well.
You can't create a packed array of bits:
struct bitmap {
bool pixel: 1 [640*480];
};
Logically, that should work, but it doesn't. Does anyone
know why?
|
I think a bitset meets that need.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Jeff Flinn Guest
|
Posted: Mon Nov 01, 2004 10:03 am Post subject: Re: Power of 2 enumerations |
|
|
"Mark" <user (AT) example (DOT) net> wrote
| Quote: | Just curious,
Is there any proposal to add the above enumerations
to C++ in future?
For example, instead of
enum
{
FEATURE1 = 1,
FEATURE2 = 2,
FEATURE3 = 4
};
we could have
typedef binary enum
{
FEATURE1,
FEATURE2,
FEATURE3
}feature_t;
|
But we already have:
typedef std::bitset<3> feature_t;
| Quote: |
void foo( feature_t f )
{
if( f & FEATURE1 )
|
if( f[0] )
| Quote: | {
}
}
int main()
{
foo( FEATURE1 | FEATURE2 );
|
foo( feature_t("011") );
| Quote: | }
so the numbers are assigned automatically and there is
no casting required to OR the values in main, ie the
typing is retained.
|
And if you want to emulate your FEATUREn instances:
const feature_t FEATURE1 = feature_t("001");
const feature_t FEATURE2 = feature_t("010");
const feature_t FEATURE3 = feature_t("100");
-----------------
Jeff Flinn
Applied Dynamics, International
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
John Nagle Guest
|
Posted: Mon Nov 01, 2004 7:54 pm Post subject: Re: Power of 2 enumerations |
|
|
Francis Glassborow wrote:
| Quote: | In article <eMahd.37057$QJ3.13078 (AT) newssvr21 (DOT) news.prodigy.com>, John
Nagle <nagle (AT) animats (DOT) com> writes
Unfortunately, bitfields and arrays don't interact well.
You can't create a packed array of bits:
struct bitmap {
bool pixel: 1 [640*480];
};
Logically, that should work, but it doesn't. Does anyone
know why?
I think a bitset meets that need.
|
It helps. But you can't declare a small field in
the middle of a record as a bitset. Bitsets are
arrays of longs, and are word aligned. Bitsets are not helpful
when you have to match some existing data format for
network or file purposes, which is what bitfields are
usually used for.
John Nagle
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Mon Nov 01, 2004 7:54 pm Post subject: Re: Power of 2 enumerations |
|
|
[email]NONONE (AT) nowhere (DOT) com[/email] ("Jeff Flinn") writes:
| Quote: | And if you want to emulate your FEATUREn instances:
const feature_t FEATURE1 = feature_t("001");
const feature_t FEATURE2 = feature_t("010");
const feature_t FEATURE3 = feature_t("100");
|
You can even write a simple template that makes these into
compile-time constants:
const feature_t FEATURE1 = binary<001>::value;
const feature_t FEATURE2 = binary<010>::value;
const feature_t FEATURE3 = binary<100>::value;
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Helium Guest
|
Posted: Mon Nov 01, 2004 7:55 pm Post subject: Re: Power of 2 enumerations |
|
|
| Quote: | assaarpa wrote:
Is there any proposal to add the above enumerations
to C++ in future?
I hope not, what you are proposing is something that can easily be done
using existing language constructs.
The problem is it's not done very well at all with
current language constructs is it?
If you want to use this idiom, you have to make these
types integers or unsigned integers, so you loose the
type safety.
The other alternative is to use ugly casts to convert
the result back into your type. Neither is a great
solution!
So is there a more elegant solution I don't know about?
|
Well, what's wrong with the suggestedone?
enum Foo {
flag1 = 1,
flag2 = 2,
flag3 = 4,
flag4 = 8
};
Foo operator | (Foo lhs, Foo rhs)
{
return static_cast<Foo>(lhs | rhs);
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Nov 02, 2004 5:43 am Post subject: Re: Power of 2 enumerations |
|
|
David Abrahams wrote:
| Quote: | NONONE (AT) nowhere (DOT) com ("Jeff Flinn") writes:
And if you want to emulate your FEATUREn instances:
const feature_t FEATURE1 = feature_t("001");
const feature_t FEATURE2 = feature_t("010");
const feature_t FEATURE3 = feature_t("100");
You can even write a simple template that makes these into
compile-time constants:
const feature_t FEATURE1 = binary<001>::value;
const feature_t FEATURE2 = binary<010>::value;
const feature_t FEATURE3 = binary<100>::value;
|
Tricky because 010 and 10 are not the same in C++. You can control
the behaviour of the class when strings are used so "010" and "10"
would mean the same, but you cannot control the behaviour of integer
literals. It would really be nice if C++ got binary literals (some
compilers had or have them), in the form of <binary_digits>b or
something of that nature. Then you can have 010b or 10b which will
be the same.
V
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Daniel Krügler Guest
|
Posted: Tue Nov 02, 2004 4:02 pm Post subject: Re: Power of 2 enumerations |
|
|
Hekllo Helium,
Helium schrieb:
| Quote: | Well, what's wrong with the suggestedone?
enum Foo {
flag1 = 1,
flag2 = 2,
flag3 = 4,
flag4 = 8
};
Foo operator | (Foo lhs, Foo rhs)
{
return static_cast<Foo>(lhs | rhs);
}
That it enforces a recursive function call? ) |
I think, you meant
Foo operator|(Foo lhs, Foo rhs)
{
return static_cast<Foo>(static_cast<unsigned>(lhs) |
static_cast<unsigned>(rhs));
}
don't you?
(It is a shame, that it currently is not possible to portably deduce the
underlying integer
type of an enumeration type, but fortunatly there are attempts to fix
this deficiency, e.g.
by Sutter et al.)
Greetings from Bremen,
Daniel
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Nov 03, 2004 7:28 pm Post subject: Re: Power of 2 enumerations |
|
|
[email]bekkah (AT) web (DOT) de[/email] (Helium) wrote in message
news:<194c1211.0411011000.726472cd (AT) posting (DOT) google.com>...
| Quote: | Well, what's wrong with the suggestedone?
enum Foo {
flag1 = 1,
flag2 = 2,
flag3 = 4,
flag4 = 8
};
Foo operator | (Foo lhs, Foo rhs)
{
return static_cast<Foo>(lhs | rhs);
}
|
Infinite recursion.
I get bitten by it almost every time. It's so easy to forget to cast
the parameters to unsigned or whatever first. And it's a potential
maintenance problem -- adding enum values can change the underlying type
of the enum, and make the original cast illegal.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Randy Maddox Guest
|
Posted: Thu Nov 04, 2004 4:53 am Post subject: Re: Power of 2 enumerations |
|
|
[email]bekkah (AT) web (DOT) de[/email] (Helium) wrote in message news:<194c1211.0411011000.726472cd (AT) posting (DOT) google.com>...
[snip]
| Quote: | So is there a more elegant solution I don't know about?
Well, what's wrong with the suggestedone?
enum Foo {
flag1 = 1,
flag2 = 2,
flag3 = 4,
flag4 = 8
};
Foo operator | (Foo lhs, Foo rhs)
{
return static_cast<Foo>(lhs | rhs);
}
|
You mean other than 5.2.9/7, which says:
A value of integral or enumeration type can be explicitly converted to
an enumeration type. The value is unchanged if the original value is
within the range of the enumeration values (7.2). Otherwise, the
resulting enumeration value is unspecified.
So that, for example, Foo flagUnspecified = flag3 | flag4 yields a
value, 12, that is not in the range of Foo enumeration values.
Randy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|