|
Boost : |
From: Edward Diener (eddielee_at_[hidden])
Date: 2003-10-04 13:41:19
In responding to Mr. Gregor's post about tribool, an idea has crossed my
mind which has occurred a few times before. Some other languages are able to
create native types which are subsets of all values of a given built-in
type. It seems as if a generic template mechanism ( or two ) should be able
to do this. As an example, for C++'s 'int' type I would like to create a
type which holds 'ints' between certain values, let's say 1 and 100 and
rejects assignments outside this range, perhaps throwing an exception if one
is attempted. I would think that template magic would be able to do this for
any given built-in integer type, and maybe for any type whatsoever if there
were a mechanism for specifying range or particular values for that type and
then checking them ( mostly likely with the equality operator ). Even beyond
range values, one should be able to create a type, through template magic,
which can only hold specific values, and even perhaps mix range values and
specific values in the definition of values that type can hold. And if one
could do this automatically through a single template mechanism based on
type, ranges of those type values, and specific values ..., that would
really be interesting. I wonder if anyone has ever thought along these lines
or found the concept useful in practice.
In reality I use property extensions in certain C++ implementations to do
this. Of course the property concept, which translates into setter and
getter calls in standard C++, is useful for other things also such as
triggering other actions when a value is set.
But I think C++ in general should be able to create types which have a
subset of values for the normal range upon which the type is based, and of
course templates are the mechanism for doing this generically. Maybe Boost
already has something like this, maybe it is easy to do for certain cases,
or maybe someone else has done this. But i am interested if anyone else has
ever found this concept useful, or has found an effective C++ solution for
it.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk