Re: Type safety, C++ and code generation




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

.



Relevant Pages

  • Re: Type safety, C++ and code generation
    ... Not mentioning the error messages that you get from the compiler ... These are exactly the factors that limit the practical useability of the template approach and that's why I claim that external code generator can be much more powerful. ...
    (comp.lang.ada)
  • Re: [C++] Vector - guaranteed to be contiguous?
    ... >> member is templated on the input iterators as well. ... template<typename OUTITER, typename INITER, typename INITERT> ... What happens is that the compiler sees copy and thinks, "Hey, I know copy. ... But it is a template, let me try to deduce those types. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: generic programming is very powerful. c should support it
    ... Where the compiler matches the correct template. ... already happens differently on different implementations. ...
    (comp.lang.c)
  • Re: General purpose Expression Parser
    ... and more generic versions of data structures and algorithms. ... template that I suddendly want to reuse for extended precision are not ... In C++ the code generation is done by the compiler, ... Compile time functions are EXECUTABLE code (i.e. already ...
    (comp.lang.c)
  • Re: .NET 2.0 to be launched on Nov 8
    ... difference in philosophy between generics and templates. ... User-defined specializations make sense in the template world ... anything which meets its constraints. ... Since the compiler can semantically check any ...
    (borland.public.delphi.non-technical)