G
Guest
OK. Try this in straight C++.Falk Tannhäuser said:Perhaps the closest way you can get to this in C++ is
std::vector<foo_type> Data;
...
std::for_each(Data.begin(), Data.end(), DoSomething);
where "DoSomething" evaluates to a so-called "function object"
having an "operator()" accepting a (reference to) "foo_type".
type Index is range -800_000..12_000_000;
type Decimal_Fraction is digits 12 range -473.0 .. 2_000.0;
type Vector is array (Index range <>) of Decimal_Fraction;
V1 : Vector ( -47..600); -- note the negative index range
V2 : Vector (-1.. 10); -- also a negative index range
V3 : Vector (42..451); -- index starts higher than zero;
...............................
function Scan (The_Vector : Vector ; Scan_Value : Decimal_Fraction )
return Natural;
-- This function can process any of those vector instances
without modification
.............................
-- possible implementation of the function
function Scan(The_Vector : Vector; Scan_Value : Decimal_Fraction )
return Natrual is
Scan_Count : Natural := 0; -- Natual begins at zero
begin
for I in The_Vector'Range -- No way to index off the end array
parameter
loop
if The_Vector(I) = Scan_Value; -- looking for an
exact match
Scan_Count := Scan_Count + 1; -- increment the count
end if;
end loop;
return Scan_Count; -- return required; never an implicit
return
end Scan;
.....................................................
I submit this in response to the observation someone made about the alleged
added
difficulty Ada imposes on the programmer in flexibility of expressiveness. In
my
own experience, Ada is far more expressive of a larger range of idioms than C++.
Note my use of the word "expressive." We can express any idea in any language,
but some languages are more expressive of some ideas than others.
This is just one example of Ada's flexibility in managing arrays. I could fill
many pages with
more examples. Of interest, here, is how easy it is to have an array index
that begins at
a value other than zero, and how easy it is to create a function that will
accept any array
of any range defined for that index. Yes, I know you can do this in C++, but
from my
perspective, it is not nearly as easy, expressive, or readable.
Counter examples to expressiveness can be illustrated in C++. For example,
many
programmers prefer the += to the x := x + 1 syntax. This is a minor
convenience
compared to the final program in Ada.
Richard Riehle
Disclaimer: I did not compile the code
before submitting it so there
might be some minor
errors. RR