Teaching new tricks to an old dog (C++ -->Ada)

  • Thread starter Turamnvia Suouriviaskimatta
  • Start date
J

Jerry Coffin

Martin Dowie wrote:

[ ... ]
But the link you give does not say that C++ was used

If I made the kind of unfair generalizations sometimes made by some Ada
advocates, I'd say that Ada programmers only look at the surface, and
don't consider the deeper meaning of things. :)

Look past the toolbox itself, and consider the systems on which it
runs. To save you the trouble of looking it up again, I'll point it
out: X running on top of either Windows or UNIX.

Now do you suppose some C and C++ might be involved?
 
I

Ioannis Vranos

Martin said:
But the link you give does not say that C++ was used - perhaps it was
(it actually says "...ODS Toolbox does not require special programming
skills in C/C++...").

But it Ada is used on all sorts of traffic systems from air to rail to
GPS. A list of such systems is available at:

http://www.act-europe.fr/aa_lookwho.php


Bottom line is both C++ and Ada are used for mission critical systems.
Also it is easy to write safe code in C++ just by using its high level
facilities, with no overflows, not anything.


For example consider this simple bullet-proof code for string input and
processing:


#include <iostream>
#include <string>

int main()
{
using namespace std;

string s;

while(cin)
{
getline(cin,s);

/*Do things with s */;
}
}
 
W

Wes Groleau

Hans said:
* case statements (Ada's equivalent of a switch in C++) are required
to handle all possible cases. Thus it is impossible to forget one.
And, of course, there is no "break;" crap in Ada.

Prevents fall through.

And it prevents forgetting to put in the break--an error
so common that the feature was explicitly prohibited in
the Ada specs.
This is more in the C++ tradition. The programmer has choice.
In C++ you can extend the type system to achieve this and more
(someone mentioned dimensional analysis), just not with typedef.

In C++ and Java, you can do a heck of a lot to program
type safety and abstraction. Problem is that it _is_
a heck of a lot -- for example, to emulate what Ada does
with things like

type Possible_Speed is range 0.0 .. 140.0;
constant Speed_Limit is 55.0;
subtype Legal_Speed is Possible_Speed range 0.0 .. Speed_Limit;
type Direction is mod 360;

V : Legal_Speed;
C, D : Direction;

Before you say it doesn't take much, remember your solution
should allow

V := Speed_Limit - 15.0;

raise an exception on
.....
V := 55.1;

and it should refuse to compile the typo

V := D;

Another problem is that because of the work involved in
enforcing abstractions with classes, most Java programmers
(probably C++, too) would rather just say

double V;
double C, D;

--
Wes Groleau

A pessimist says the glass is half empty.

An optimist says the glass is half full.

An engineer says somebody made the glass
twice as big as it needed to be.
 
W

Wes Groleau

The simple loop will always be an infinite loop unless
you break out early. The Ada reserved word used to
break out of a loop is "exit". In Ada "exit" does not
exit the program, only the enlcosing loop.

Or (for nested loops), each loop can have a name,
and the exit statement can exit out of two or more nested
loops by using the name of the level to get out of.

Java has a construct that can do something like that.
Does C++ ?
 
J

Jerry Coffin

Wes Groleau wrote:

[ ... ]
Or (for nested loops), each loop can have a name,
and the exit statement can exit out of two or more nested
loops by using the name of the level to get out of.

Java has a construct that can do something like that.
Does C++ ?

Thankfully, no. IMO, both Java and Ada would be improved by elimination
of this misfeature. Even though C++'s 'break' is simpler, I'd say less
than 10% of the time I see it used to exit loops is justified. That's
not to say that there's NO good use for it, only that it's used FAR
more often than justified.
 
W

Wes Groleau

Jerry said:
Thankfully, no. IMO, both Java and Ada would be improved by elimination
of this misfeature. Even though C++'s 'break' is simpler, I'd say less
than 10% of the time I see it used to exit loops is justified. That's
not to say that there's NO good use for it, only that it's used FAR
more often than justified.

Interesting. Ada is bad because you _think_ it won't let you do what
you want to do. But it would be better if it prevented other people
from doing something you think they shouldn't do.

--
Wes Groleau
"To know what you prefer, instead of humbly saying
Amen to what the world tells you you should prefer,
is to have kept your soul alive."
-- Robert Louis Stevenson
 
I

Ioannis Vranos

Pascal said:
Ok, thanks. This is definitly a recursive definition. AFAIK there is no such
thing in Ada.


This is compile-time "template metaprogramming". An entire field of its own.


There are two kinds of recursion with templates, run-time instance
recursion (as usual) and compile-time recursion.
 
I

Ioannis Vranos

Wes said:
Or (for nested loops), each loop can have a name,
and the exit statement can exit out of two or more nested
loops by using the name of the level to get out of.

Java has a construct that can do something like that.
Does C++ ?


There is no need for such a thing, and you *can* do this and other ugly
stuff in C++ by using goto.
 
J

Jim Rogers

Of course you can!


Can this be done in Ada?


#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm>


int main()
{
using namespace std;

vector<int> vec(1000);

// Seeds the random number generator
srand(time(0));

// Use rand() to fill vector with values
// As you see the operation is entirely safe.
generate(vec.begin(), vec.end(), rand);

vector<int>::size_type i;

// Finds the first index where a value is smaller than 1000
// in low level style
for(i=0; i<vec.size(); ++i)
if(vec<1000)
break;

i==vec.size()? cout<<"No number <1000 was found\n"
:cout<<"Number <1000 found at index "<<i<<"\n";
}


The idioms are somewhat different. For instance, the Ada for loop
does not iterate one past the end of the array, nor is the
value of the loop control variable visible outside the loop.

I did not see where you set the range of values for your
random number type. I am generating integer values in the
range of 0 through 10,000.

with Ada.Text_Io;
with Ada.Numerics.Discrete_Random;

procedure Random_Fill is
type Rand_Values is range 0..10_000;
package My_Rand is new Ada.Numerics.Discrete_Random(Rand_Values);

Seed : My_Rand.Generator;
subtype Index_Type is Natural range 1..1000;
Vec : array(Index_Type) of Rand_Values;
Found : Natural := 0;
begin
My_Rand.Reset(Seed); -- seeds the random number generator

-- fill the array with random values in the range of 0
-- through 10,000
for I in Vec'range loop
Vec(I) := My_Rand.Random(Seed);
end loop;

-- finds first index where a value is less than 1000
for I in Vec'range loop
if Vec(I) < 1000 then
Found := I;
exit;
end if;
end loop;
if Found > 0 then
Ada.Text_Io.Put_Line("Number < 1000 found at index" &
Natural'Image(Found));
else
Ada.Text_Io.Put_Line("No number < 1000 was found");
end if;
end Random_Fill;

Jim Rogers
 
I

Ioannis Vranos

Falk 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".



Yes you can write completely safe, bullet-proof code in C++ and without
sacrificing efficiency (and some times even improving it!) by using the
high level C++ constructs. Examples are: find family (which includes
find_if etc), search family, generate, fill, for_each, count, mismatch,
equal, transform, copy, swap, replace, remove, unique, rotate, sort,
bound, merge, partition, includes, set (like set_union), heap (like
make_heap - makes sequences behave as heaps), comparisons (like min,
max, lexicographical_compare), permutation, etc families.


We can say it is an entirely completely safe, high level language of its
own, and in this way you may never use loops for most things!
 
I

Ioannis Vranos

Jim said:
#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm>


int main()
{
using namespace std;

vector<int> vec(1000);

// Seeds the random number generator
srand(time(0));

// Use rand() to fill vector with values
// As you see the operation is entirely safe.
generate(vec.begin(), vec.end(), rand);

vector<int>::size_type i;

// Finds the first index where a value is smaller than 1000
// in low level style
for(i=0; i<vec.size(); ++i)
if(vec<1000)
break;

i==vec.size()? cout<<"No number <1000 was found\n"
:cout<<"Number <1000 found at index "<<i<<"\n";
}



The idioms are somewhat different. For instance, the Ada for loop
does not iterate one past the end of the array, nor is the
value of the loop control variable visible outside the loop.

I did not see where you set the range of values for your
random number type. I am generating integer values in the
range of 0 through 10,000.



I used the default range.

with Ada.Text_Io;
with Ada.Numerics.Discrete_Random;

procedure Random_Fill is
type Rand_Values is range 0..10_000;
package My_Rand is new Ada.Numerics.Discrete_Random(Rand_Values);

Seed : My_Rand.Generator;
subtype Index_Type is Natural range 1..1000;
Vec : array(Index_Type) of Rand_Values;
Found : Natural := 0;
begin
My_Rand.Reset(Seed); -- seeds the random number generator

-- fill the array with random values in the range of 0
-- through 10,000
for I in Vec'range loop
Vec(I) := My_Rand.Random(Seed);
end loop;

-- finds first index where a value is less than 1000
for I in Vec'range loop
if Vec(I) < 1000 then
Found := I;
exit;
end if;
end loop;
if Found > 0 then
Ada.Text_Io.Put_Line("Number < 1000 found at index" &
Natural'Image(Found));
else
Ada.Text_Io.Put_Line("No number < 1000 was found");
end if;
end Random_Fill;


Yes but what happens if you want to use the indexed found with the
associated value (ot could be inside a string for example) in an
associative container like a map?

That is, find the index and then use the index to add the value in a
map. I suppose you would create a separate procedure for this in which
you would pass the array along with the container and perhaps along with
the item to be found, just for one such operation!
 
I

Ioannis Vranos

Ioannis said:
Yes you can write completely safe, bullet-proof code in C++ and without
sacrificing efficiency (and some times even improving it!) by using the
high level C++ constructs. Examples are: find family (which includes
find_if etc), search family, generate, fill, for_each, count, mismatch,
equal, transform, copy, swap, replace, remove, unique, rotate, sort,
bound, merge, partition, includes, set (like set_union), heap (like
make_heap - makes sequences behave as heaps), comparisons (like min,
max, lexicographical_compare), permutation, etc families.


We can say it is an entirely completely safe, high level language of its
own, and in this way you may never use loops for most things!


More accurately described as "entirely completely safe, high level world
of its own".
 
L

Larry Kilgallen


The VMS primitive debugger Delta (used to debug the system bootstrap
but not much else) is famous for having just one error message:

Eh ?

At one point somebody inside DEC created a fake Delta support Notes
conference with various questions about the way Delta works, each
receiving the response:

Eh ?
 
M

Martin Dowie

Jerry said:
If I made the kind of unfair generalizations sometimes made by some Ada
advocates, I'd say that Ada programmers only look at the surface, and
don't consider the deeper meaning of things. :)

Look past the toolbox itself, and consider the systems on which it
runs. To save you the trouble of looking it up again, I'll point it
out: X running on top of either Windows or UNIX.

Now do you suppose some C and C++ might be involved?

Sorry, but if you make a link to a news item /about/ a particular tool,
at least /I/ am going to believe that you are making a point about the
tool and not the OS it works on! Why not provide a link directly to a
page about X running on top of either Windows or UNIX?! :)

Cheers

-- Martin
 
J

Jim Rogers

Yes but what happens if you want to use the indexed found with the
associated value (ot could be inside a string for example) in an
associative container like a map?

That is, find the index and then use the index to add the value in a
map. I suppose you would create a separate procedure for this in which
you would pass the array along with the container and perhaps along with
the item to be found, just for one such operation!

I assume the container type is already defined, including a procedure
to insert new values into the container. I would simply call the
insert or add procedure for that container, passing in the
appropriate information. This is no different than calling a
member function for a class in C++.

Jim Rogers
 
H

Hans Malherbe

Goeie dag Georg

Thanks for all the replies, I'll stop wasting bandwith with trivial
questions and go find out myself.

Groete
Hans
 
I

Ioannis Vranos

Jim said:
I assume the container type is already defined, including a procedure
to insert new values into the container. I would simply call the
insert or add procedure for that container, passing in the
appropriate information. This is no different than calling a
member function for a class in C++.


How can this be done in Ada?


As it can be seen here, the bullet-proof high level facilities are used. :)


#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm>
#include <functional>
#include <map>


int main()
{
using namespace std;

vector<int> vec(1000);

map<int, int> iimap;

// Seeds the random number generator
srand(time(0));

// Use rand() to fill vector with values
// As you see the operation is entirely safe.
generate(vec.begin(), vec.end(), rand);


// Finds the first occurrence of an int smaller than 1000
vector<int>::const_iterator p= find_if(vec.begin(), vec.end(),
bind2nd(less<int>(), 1000) );

int index= p-vec.begin();

// Stores the index and the value in a map
iimap[index]= *p;


cout<<index<<":\t"<<iimap[index]<<"\n";
}
 
G

Guest

Wes said:
Or (for nested loops), each loop can have a name,
and the exit statement can exit out of two or more nested
loops by using the name of the level to get out of.

Java has a construct that can do something like that.
Does C++ ?

No. However, a 'return' from the middle of a function
(even from within the deepest nested loops) is possible.

Another way, especially designed for error handling, are
exceptions which allow "returning" from many function call
levels while cleaning up and leaving the program in a well-
defined, clean state through calling the destructors of all
stack-based objects on the way. However this requires some
care from the programmer - you have to code in an "exception-
safe" manner.
I believe Ada also has exceptions, but I don't know if
they work in the same manner. Does Ada have some equivalent
of C++ constructors and destructors?

Falk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,205
Messages
2,571,067
Members
47,673
Latest member
MahaliaPal

Latest Threads

Top