Re: where exactly c++,c fail and Ada gets thru'




Martin Krischik wrote:
Maciej Sobczak wrote:

Yes, classes are used to define new types. Is there any problem with
this? It is also necessary in Ada if we want to do just a bit more of
what is provided by type and subtype constructs.

Say:

type Y_Type is range 1 .. 10;
subtype X_Type is new x range 1 .. 5;

function F (X: X_Type) return Y_Type is
begin
return X + 3;
end F

Now I would expect that an Ada compiler will not insert any runtime checks
as X + 3 is allways in the range of Y_Type. However:

function G (X: X_Type) return Y_Type is
begin
return X + 7;
end G;

may well be outside the range of Y_Type and if so CONSTRAINT_ERROR will be
raised.

Now say we have a suitable C++ range template:

typedef range<int, 1, 10> Y_Type;
typedef range<Y_Type, 1, 5> X_Type;

Y_Type F (X_Type X)
{
return X + 3;
}

Y_Type G (X_Type X)
{
return X + 7;
}

Now anyone up to the challenge to define:

template <typename Base_Type, Base_Type First, Base_Type Last>
range::operator = (Base_Type right)

template <typename Base_Type, Base_Type First, Base_Type Last>
range::operator + (Base_Type right)

template <typename Base_Type, Base_Type First, Base_Type Last>
range::range (Base_Type right)

in such a way that it works like Ada - inclusive the *realistic* change that
the compiler will optimize away the range check in F ().

I have done this.

.



Relevant Pages

  • Re: Extending discriminant types
    ... direct from Adacore. ... that was years after the RM and AARM for Ada 95 was written. ... access subtype, this should not affect any existing programs. ... The Annotated Ada Reference Manual does clarify this, ...
    (comp.lang.ada)
  • Re: Ranges and (non)static constraints
    ... In the type declaration I think the semantics of Ada ... seem to be wanting - but compiler writers don't seem to be operating in that ... subtype S is T range 1..N-1; ... language concepts and practical implementations (note ...
    (comp.lang.ada)
  • Re: types and subtypes
    ... If an Ada subtype adds a constraint to an Ada type, ... representation and be substitutable for the base one. ... Downward conversions additionally check the constraint. ...
    (comp.lang.ada)
  • Re: Ranges and (non)static constraints
    ... Why and what is the real difference between T and U? ... an upper limit at some point. ... and Ada is one of the best choices ... subtype T is Biggest_T range 1..N; ...
    (comp.lang.ada)
  • Re: Ada OOP syntax
    ... That syntax seems to be clearer than tagged types. ... I think because this idea would have turned Ada inside out. ... Also the word subtype as used in "package subtype" has a fundamentally ...
    (comp.lang.ada)