From: "REH" <spamjunk@stny.rr.com>
Subject: Re: Type safety, C++ and code generation
Date: 27 Apr 2006 09:05:04 -0700
Date: 2006-04-27T09:05:04-07:00 [thread overview]
Message-ID: <1146153904.898288.238600@y43g2000cwc.googlegroups.com> (raw)
In-Reply-To: <e2qn8t$n13$1@sunnews.cern.ch>
Maciej Sobczak wrote:
> REH wrote:
>
> > I don't see where you've "done that."
>
> The template class that implements range checking?
NO, a class that uses template to ELIMINATE unnecessary checks.
> What about making different types really distinct?
>
> typedef ranged_type<int, 0, 100> R1;
> typedef ranged_type<int, 0, 100> R2;
> typedef ranged_type<int, 0, 101> R3;
My code uses this technique if you want truely unqiue types:
class R1_unique{};
class R2_unique{};
typedef ranged_type<int, 0, 100, R1_unique> R1;
typedef ranged_type<int, 0, 100, R2_unique> R2;
>
> Another problem is that the range, as a set of values, is not the only
> thing that constitues a type - you also need to define the set of legal
> operations. The problem is that I usually want them to be different in
> each type.
> Consider this:
>
> type ranged_type<int, 0, 250> Speed;
> Speed s1, s2, s3; // with some values
> s1 = s2 + s3; // OK
> s1 = s2 * s3; // not OK
>
> The addition is fine, but the multiplication should not be provided,
> because speed multiplied by speed is not a speed. Can you extend your
> class so that the compiler will refuse to compile the second operation
> above?
Yes, you derive from the class and put the multiplication operator in
the private scope.
> Another problem is variation of the behaviour in the out-of-range
> condition. What should happen then? Throw an exception? That's only one
> of at least four different options I can imagine, and also not the one I
> would choose most of the time. Does your class allow variations here?
Yes, the template takes a traits class. If allow modification of
various behaviors, such as what should be done with an out-of-range
value, an overflow condition, a divide-by-zero, etc.
>
> And so on - all this *can* be done with templates, but at some point
> this approach just saturates and the code becomes a nightmare. Not
> mentioning the error messages that you get from the compiler (and the
> whole purpose of this is to actually get *useful* error messages,
> right?). And not mentioning the compilation time.
I've heard all these things. That may have been true with older
compilers. They are a lot better today.
REH
next prev parent reply other threads:[~2006-04-27 16:05 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-04-27 10:35 Type safety, C++ and code generation Maciej Sobczak
2006-04-27 13:19 ` REH
2006-04-27 14:08 ` Maciej Sobczak
2006-04-27 14:33 ` REH
2006-04-27 15:16 ` Maciej Sobczak
2006-04-27 15:39 ` Georg Bauhaus
2006-04-28 5:57 ` Maciej Sobczak
2006-04-28 12:16 ` Georg Bauhaus
2006-04-28 12:37 ` brian.b.mcguinness
2006-04-28 17:04 ` Georg Bauhaus
2006-04-28 18:50 ` Jeffrey R. Carter
2006-04-28 19:07 ` Randy Brukardt
2006-04-28 19:46 ` Robert A Duff
2006-04-28 19:55 ` Dmitry A. Kazakov
2006-04-29 21:28 ` Florian Weimer
2006-04-27 16:05 ` REH [this message]
2006-04-28 6:17 ` Maciej Sobczak
2006-04-30 14:40 ` adaworks
2006-04-30 15:23 ` REH
2006-04-30 18:14 ` Martin Krischik
2006-05-02 6:27 ` Maciej Sobczak
2006-05-02 13:33 ` REH
2006-05-02 15:05 ` Maciej Sobczak
2006-05-02 15:54 ` REH
2006-05-02 16:50 ` Martin Krischik
2006-05-02 19:50 ` REH
2006-04-27 14:47 ` REH
2006-04-27 15:10 ` REH
2006-04-28 6:22 ` Maciej Sobczak
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox