Topic: C++ seems to be missing an important feature : Min and Max Values of Enumeration
Author: Chris Ahlstrom <ahlstroc@bellsouth.net>
Date: 1998/11/08 Raw View
So you're saying that the compiler should provide special support for one
of the special uses of the enum -- to provide a continous integer domain
bounded by a min and max value.
Can you provide a brief specification (with notational convention) for
this feature?
Chris
Elron A. Yellin wrote:
> > Personally, I /like/ C and C++'s leaving some of these decisions to the
> > programmer. Otherwise, we'd end up putting up with mandatory
> > garbage collection and such truck.
>
> In the case of enum min/max I disagree.
> Any information the compiler has at compile time should be accessible to
> the programmer so that compile-time or runtime assertions can be added to
> code.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain.COM (Siemel Naran)
Date: 1998/11/09 Raw View
On 08 Nov 98 22:28:40 GMT, Chris Ahlstrom <ahlstroc@bellsouth.net> wrote:
>So you're saying that the compiler should provide special support for one
>of the special uses of the enum -- to provide a continous integer domain
>bounded by a min and max value.
>
>Can you provide a brief specification (with notational convention) for
>this feature?
Yes. Each enum defines a typedef utype that gives the underlying
type of the enum. Eg, utype could be "int", "unsigned", "char".
But if the range of the enum is bigger than can fit into an "unsigned",
then utype will have to be something else. Perhaps a std::bitset.
This is a problem. Anyway, each enum will define three static const
variables of type utype called "min" and "max" and "mask". Eg,
enum E { e0=0x01, e1=0x02, e2=0x04 };
int main()
{
using std::cout;
cout << typeid(E::utype).name() << '\n'; // may print "unsigned char"
cout << E::max << '\n'; // calls ostream::op<<(unsigned char) to print "1"
cout << E::min << '\n'; // prints "4"
cout << E::mask << '\n'; // prints "7"
}
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: dietmar.kuehl@claas-solutions.de
Date: 1998/11/09 Raw View
Hi,
Chris Ahlstrom (ahlstroc@bellsouth.net) wrote:
> So you're saying that the compiler should provide special support for one
> of the special uses of the enum -- to provide a continous integer domain
> bounded by a min and max value.
Sounds reasonable, at least to me: For the other integer types, there is such
a thing, why not for enumerations? The only question is whether the min and
max values should be inclusive of those implicitly available or exclusive.
Actually, I guess that both are occasionally useful and probably both would
thus be desirable.
> Can you provide a brief specification (with notational convention) for
> this feature?
That's the easy part! Here we go:
If 'E' is an 'enum' with 'E_min' being the minimum value of type 'E' and
'E_max' being the maximum value of the type 'E' (both should be the values
explicitly appearing in the 'enum' definition), the following should be true:
// values available for other numeric types:
std::numeric_limits<E>::is_specialized == true std::numeric_limits<E>::min()
== E_min std::numeric_limits<E>::max() == E_max
std::numeric_limits<E>::digits == # of binary digits used for representation
std::numeric_limits<E>::digits10 == # of decimal digits used for E_max_rep
std::numeric_limits<E>::signed == E_min < 0
std::numeric_limits<E>::is_integer == true std::numeric_limits<E>::is_exact
== true std::numeric_limits<E>::radix == 2
// special values for enumeration types:
std::numeric_limits<E>::inclusive_min() == E_min_rep // see below
std::numeric_limits<E>::inclusive_max() == E_max_rep // see below
Most of these values are already described in the standard (in Section
lib.support.limits). This interface is thus rather obvious. Only two members
are added to the "normal" set of values in 'numeric_limits'. Here is the
rationale for them: An enumeration value can store at least a range of values
starting with the minimum value in the enumeration and ending with the
maximum value of the enumeration, independent on whether there are names for
the values in between these extremes. However, it is defined that an
enumeration value can store even values falling outside this range, if the
corresponding bits allow this. For example, for this enum enum E { min = 3,
max = 5 }; there are at least three bits used. However, the range of values
which can be represented using three bits ranges from 0 to 7 not just from 3
to 5. 'inclusive_min()' is the lowest and 'inclusive_max()' is the largest of
these values (I'm not sure whether these names are really appropriate,
though).
The implementation of these numeric traits can be done at no cost, if they are
not used: The compiler can detect that a value of 'numeric_limits' for an
enumeration type is accessed and substitute the corresponding value. This is,
however, rather demanding for the compiler (well, actually, I have no clue how
much work it would be to implement this feature). An easier implementation
method which would use some memory for every enum would be to generate the
corresponding signatures as soon as an 'enum' is seen.
It might also be useful to get access to the name/value pairs of values
declared in the enumeration definition. This goes into the direction of
getting information about types into the language. Although I think that this
might also be very useful, it a wide field which needs more thought before
proposing a corresponding feature and its syntax.
In any case, to push something like this into the next version of the
standard, two things are necessary: 1. A proposal has to be submitted to the
committee. 2. At least one sample implementation has to be available which
demonstrates that this can be implemented and that it is useful.
BTW, assuming that both a well worked out proposal for an extension is
submitted to the committee and a compiler implementing the proposed stuff is
shown, will the proposal be considered for C++ 200X? Where do I have to sent
proposals to? When will the committee start to accept proposals for
extensions and when will the committee start to deal with them?
However, while your compiler does not support this yet, you can emulate it
easily by providing appropriate specializations of the 'numeric_limits' class
manually (potentially this is sufficient anyway).
--
<mailto:dietmar.kuehl@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/11/10 Raw View
dietmar.kuehl@claas-solutions.de wrote:
....
> However, while your compiler does not support this yet, you can emulate it
> easily by providing appropriate specializations of the 'numeric_limits' class
> manually (potentially this is sufficient anyway).
However, such specializations would violate a 'shall' condition in the
standard, as I described in another message.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Chris Ahlstrom <ahlstroc@bellsouth.net>
Date: 1998/11/06 Raw View
If that is true, then why isn't it a language feature? Was it seen as of
limited useful, difficult implementation, difficult specification,
or was it an efficiency issue?
Personally, I /like/ C and C++'s leaving some of these decisions to the
programmer. Otherwise, we'd end up putting up with mandatory
garbage collection and such truck.
Chris
James Kuyper wrote:
> Chris Ahlstrom wrote:
> >
> > Wrapper class. Define min() and max() functions.
> > Can add range-checking too. For most purposes,
> > defining your own min and max values is enough.
>
> That's far more complicated than necessary; just add minimum and maximum
> enumerator values. The practical problem lies in guarantying that they
> are truly minumum and maximum, but that applies to min() and max()
> functions, too. A language feature comparable to numeric_traits<> would
> be better.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Chris Ahlstrom <ahlstroc@bellsouth.net>
Date: 1998/11/06 Raw View
This is my habit also. On rare occasions, I'll feel compelled to write
a class to enforce the issues. As the private part of a class,
it's pretty easy to cut-and-paste (in virtual sense) this simple
code.
Chris
David R Tribble wrote:
> I employ the practice of adding minimum and maximum values in most
> of my enum declarations. For example:
>
> enum Status
> {
> ST_UNDEF = 0,
> ST_INIT,
> ST_OPEN,
> ...
> ST_CLOSE,
> ST_MAX
> };
>
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: elron@alum.mit.edu (Elron A. Yellin)
Date: 1998/11/07 Raw View
In article <3642F398.DDF27A60@bellsouth.net>, kisov@bellsouth.net wrote:
> Personally, I /like/ C and C++'s leaving some of these decisions to the
> programmer. Otherwise, we'd end up putting up with mandatory
> garbage collection and such truck.
In the case of enum min/max I disagree.
Any information the compiler has at compile time should be accessible to
the programmer so that compile-time or runtime assertions can be added to
code.
Adding your own min/max constants or functions is not foolproof.
Elron A. Yellin
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/11/07 Raw View
Chris Ahlstrom wrote:
>
> If that is true, then why isn't it a language feature? Was it seen as of
> limited useful, difficult implementation, difficult specification,
> or was it an efficiency issue?
>
> Personally, I /like/ C and C++'s leaving some of these decisions to the
> programmer. Otherwise, we'd end up putting up with mandatory
> garbage collection and such truck.
>
> Chris
>
> James Kuyper wrote:
>
> > Chris Ahlstrom wrote:
> > >
> > > Wrapper class. Define min() and max() functions.
> > > Can add range-checking too. For most purposes,
> > > defining your own min and max values is enough.
> >
> > That's far more complicated than necessary; just add minimum and maximum
> > enumerator values. The practical problem lies in guarantying that they
> > are truly minumum and maximum, but that applies to min() and max()
> > functions, too. A language feature comparable to numeric_traits<> would
> > be better.
The standard requires that numeric_traits<> must be specialized for each
standard numeric type. My first thought was that this should be extended
to include enumerations. Then I remembered that each enumeration is a
seperate type, so that implementing such a thing would require a fair
amount of compiler magic. Essentially, the compiler would have to insert
the equivalent of a numeric_traits<> specialization for each enumeration
type that gets declared. That's probably too messy. Still, it would be
convenient.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1998/11/03 Raw View
Hi,
I would like to get min and max values in enumeration.
================ Example ==================
enum fee
{
eee1 = 3,
eee2,
eee3,
eee4 = 20,
eee5
};
Min value == 3,
Max value == 21
===========================================
Are there such functions in any library ?
Then we could use them as following :
min_enum_value (fee) == 3,
max_enum_value (fee) == 21.
Thanks,
Alex
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/11/04 Raw View
Alex Vinokur <alexander.vinokur@telrad.co.il> writes:
>I would like to get min and max values in enumeration.
>================ Example ==================
>enum fee
>{
> eee1 = 3,
> eee2,
> eee3,
> eee4 = 20,
> eee5
>};
>Min value == 3,
>Max value == 21
>===========================================
>Are there such functions in any library ?
No. A language mechanism would be required, and there
isn't one in C++.
You can use a coding discipline to work around the problem,
such as by adding "max" and "min" enumerators. But even
that might not be sufficient.
Example:
enum foo {
f1 = 9, f2 = 7, foo_min = f2, f3 = 100, foo_max = f3, f4 = 90
};
const foo f5 = foo(2); // smaller than foo_min
const foo f6 = foo(127); // larger than foo_max
The C++ standard allows you to create pseudo-enumerators
different from the true enumerators, as long as they fit
in the extended range of the enum type. To find the
extended range, compute the number of bits needed to
represent all the enumerators as binary numbers. Any other
number that can be represented in that number of bits is
a valid value for constants and objects of the enum type.
In my example, the first enumerator is not the smallest
value and the last is not the largest. I added min and
max enumerators to show the smallest and largest values,
but then created pseudo-enumerators outside that range.
Enums in C and C++ don't have the strongly-typed properties
that you would need to get the effect you want.
--
Steve Clamage, stephen.clamage@sun.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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Chris Ahlstrom <ahlstroc@bellsouth.net>
Date: 1998/11/05 Raw View
Wrapper class. Define min() and max() functions.
Can add range-checking too. For most purposes,
defining your own min and max values is enough.
Debuggers (e.g. Visual C's) will complain when they
see an out-of-range value.
In my opinion, it is good that there are many things
in C++ that one has to write for oneself. Learn more
that way.
Chris
Steve Clamage wrote:
> Alex Vinokur <alexander.vinokur@telrad.co.il> writes:
>
> >I would like to get min and max values in enumeration.
>
> >================ Example ==================
> >enum fee
> >{
> > eee1 = 3,
> > eee2,
> > eee3,
> > eee4 = 20,
> > eee5
> >};
--
Chris Ahlstrom
major league software, llc
1259 Center Lake Dr.
Mt. Pleasant, SC 29464-7419
(843)849-0985
a h l s t r o c @bellsouth.net
My wife is a Windows NT widow!
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <david.tribble@noSPAM.central.beasys.com>
Date: 1998/11/05 Raw View
Alex Vinokur <alexander.vinokur@telrad.co.il> writes:
>> I would like to get min and max values in enumeration.
>>
>> enum fee
>> {
>> eee1 = 3,
>> eee2,
>> eee3,
>> eee4 = 20,
>> eee5
>> };
>>
>> Min value == 3,
>> Max value == 21
>>
>> Are there such functions in any library?
Steve Clamage wrote:
> No. A language mechanism would be required, and there
> isn't one in C++.
>
> You can use a coding discipline to work around the problem,
> such as by adding "max" and "min" enumerators. But even
> that might not be sufficient.
>
[snip]
>
> Enums in C and C++ don't have the strongly-typed properties
> that you would need to get the effect you want.
I employ the practice of adding minimum and maximum values in most
of my enum declarations. For example:
enum Status
{
ST_UNDEF = 0,
ST_INIT,
ST_OPEN,
...
ST_CLOSE,
ST_MAX
};
When I construct/initialize an object of type 'Status', I set it
to ST_UNDEF; this indicates that the object hasn't been set to a
valid value yet. If I need to check for a valid Status value, I
can check that it's within the range (ST_UNDEF,ST_MAX); this lets
me know if I've got an erroneous object value. I can also iterate
through the Status values, from ST_UNDEF+1 to ST_MAX-1 (if I use
them as array indicies, for example).
In other words, the ST_UNDEF and ST_MAX enumeration constants act
as minimum and maximum sentinel values for the enumeration type.
(Obviously, something this straightforward won't work for complex
enum declarations with non-monotonically increasing values.)
(BTW, I used to use names like 'ST__UNDEF' and 'ST__MAX' to make
them stand out, but names with two consecutive underscores are now
reserved for the implementation.)
-- David R. Tribble, dtribble@technologist.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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/11/05 Raw View
Chris Ahlstrom wrote:
>
> Wrapper class. Define min() and max() functions.
> Can add range-checking too. For most purposes,
> defining your own min and max values is enough.
That's far more complicated than necessary; just add minimum and maximum
enumerator values. The practical problem lies in guarantying that they
are truly minumum and maximum, but that applies to min() and max()
functions, too. A language feature comparable to numeric_traits<> would
be better.
...
> > Alex Vinokur <alexander.vinokur@telrad.co.il> writes:
> >
> > >I would like to get min and max values in enumeration.
> >
> > >================ Example ==================
> > >enum fee
> > >{
> > > eee1 = 3,
> > > eee2,
> > > eee3,
> > > eee4 = 20,
> > > eee5
> > >};
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]