473,796 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I 'm following various posting in "comp.lang. ada, comp.lang.c++ ,
comp.realtime, comp.software-eng" groups regarding selection of a
programming language of C, C++ or Ada for safety critical real-time
applications. The majority of expert/people recommend Ada for safety
critical real-time applications. I've many years of experience in C/C++ (and
Delphi) but no Ada knowledge.

May I ask if it is too difficult to move from C/C++ to Ada?
What is the best way of learning Ada for a C/C++ programmer?

Jul 23 '05
822 29831
On Sat, 12 Mar 2005 09:42:26 +0100, Georg Bauhaus wrote:
Falk Tannhäuser wrote:
Dr. Adrian Wrigley wrote:
But what of features not present in either?


[...]
associative arrays (from Perl)


Wouldn't that be std::map in C++?


and in Ada 2005,

Ada.Containers. Hashed_Maps and Ada.Containers. Hashed_Maps


I have probably missed a trick in the C++, but I couldn't get
std::map code to compile (except in the trivial cases):

#include <map>

struct compoundindex {
int a, b, c;
};

int main()
{
std::map<compou ndindex, float> hash;
compoundindex fred = { 1, 2, 4 };

hash[fred] = 0.123;
}

cpptest.cpp: In function `int main()':
cpptest.cpp:14: error: `main()::compou ndindex' uses local type
`main()::compou ndindex'
(8 more lines of errors here!)

(what is the simplest C++ code to get this intent?)
--
The Ada associative arrays from the new draft standard
are specified as something like:

generic
type Key_Type is private;
type Element_Type is private;
with function Hash (Key : Key_Type)
return Hash_Type is <>;
with function Is_Equal_Key (Left, Right : Key_Type)
return Boolean is "=";
with function "=" (Left, Right : Element_Type)
return Boolean is <>;
package Ada.Containers. Hashed_Maps is...
Which clearly won't work unless you can find the three
function generic parameters. I don't see how this can be
used easily in a generic context.

I don't think I am being *that* unreasonable in asking for arrays
indexed by arbitrary types, without jumping through hoops :)
--
Adrian

Jul 23 '05 #421
On Sat, 12 Mar 2005 12:59:31 -0500, CTips wrote:
Robert A Duff wrote:
Ki*******@SpamC op.net (Larry Kilgallen) writes:

Even Bliss has nested functions. What Ada has that Pascal has in addition
to nested functions is uplevel addressing, allowing an inner function to
access data declared in outer scopes.

Heh? Pascal has that. In fact, practically every programming language
outside the C family has this feature. It's quite useful -- almost
essential in multi-threaded programs.

- Bob


Yeah, and don't ask what it costs you. I'd carefully forgotten about all
the grungy details about displays and static/dynamic chaining, and you
had to remind me. I particularily like solutions that reserves a
register for the top-of-display/top-of-static-chain. Thats right - blow
away a register for that. And then of course the cost of
maintaining/walking those structures.

If you need thread-private storage, there are *much* cheaper solutions.


isn't uplevel addressing usually zero cost? Are you saying it is
expensive whenever you use it? Or expensive on all programs, whether
or not it is used? Is it absent from C++ because of cost?
(I'm sure Robert understands this far better than I!)
--
Adrian

Jul 23 '05 #422
Dr. Adrian Wrigley wrote:
#include <map>

struct compoundindex {
int a, b, c;
};

inline bool operator <(const compoundindex &a, const compoundindex &b)
{
return (a.a<b.a && a.b<b.b && a.c<b.c)? true: false;
}

int main()
{
std::map<compou ndindex, float> hash;
compoundindex fred = { 1, 2, 4 };

hash[fred] = 0.123;
}


As a rule of thumb remember that most standard containers and algorithms
require the presence of operator <.
From a design perspective, in the example above, it is better to make
operator< a member of the struct. I made it a global function, in case
you want a POD struct.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #423

"Dr. Adrian Wrigley" <am**@linuxchip .demon.co.uk.uk .uk> skrev i en
meddelelse
news:pa******** *************** *****@linuxchip .demon.co.uk.uk .uk...
On Sat, 12 Mar 2005 09:42:26 +0100, Georg Bauhaus wrote:
Falk Tannhäuser wrote:
Dr. Adrian Wrigley wrote:

But what of features not present in either?

[...]

associative arrays (from Perl)

Wouldn't that be std::map in C++?
and in Ada 2005,

Ada.Containers. Hashed_Maps and Ada.Containers. Hashed_Maps


I have probably missed a trick in the C++, but I couldn't get
std::map code to compile (except in the trivial cases):

#include <map>

struct compoundindex {
int a, b, c;
};

int main()
{
std::map<compou ndindex, float> hash;
compoundindex fred = { 1, 2, 4 };

hash[fred] = 0.123;


First off, std::map is not hash-based. It is a sorted container. Now in
order to use this, you need a comparing function. It can be the "standard"
ordering function for your key or it can be a user specified function.
}

cpptest.cpp: In function `int main()':
cpptest.cpp:14: error: `main()::compou ndindex' uses local type
`main()::compou ndindex'
(8 more lines of errors here!)
You must have snipped the most interesting errormessage.
(what is the simplest C++ code to get this intent?)
What you need here is a comparator:

bool operator<(compo undindex const& lhs,compoundind ex const& rhs)
{
.....
}

And the map should work. --
The Ada associative arrays from the new draft standard
are specified as something like:

generic
type Key_Type is private;
type Element_Type is private;
with function Hash (Key : Key_Type)
return Hash_Type is <>;
with function Is_Equal_Key (Left, Right : Key_Type)
return Boolean is "=";
with function "=" (Left, Right : Element_Type)
return Boolean is <>;
package Ada.Containers. Hashed_Maps is...
Which clearly won't work unless you can find the three
function generic parameters. I don't see how this can be
used easily in a generic context.

I don't think I am being *that* unreasonable in asking for arrays
indexed by arbitrary types, without jumping through hoops :)
But I do! How can you index without at least knowing if two values are
different or the same?
The C++ standard will have a hash-based container later. Those interested in
having one can download one from e.g. boost (I believe this container also
to be implemented by boost and prbably several others).
--
Adrian

/Peter
Jul 23 '05 #424
Dr. Adrian Wrigley wrote:
I have probably missed a trick in the C++, but I couldn't get
std::map code to compile (except in the trivial cases):

#include <map>

struct compoundindex {
int a, b, c;
};

int main()
{
std::map<compou ndindex, float> hash;
compoundindex fred = { 1, 2, 4 };

hash[fred] = 0.123;
}


C++ associative containers (map, set, multimap, multiset) require
a "strict weak ordering" for their key types. In the present case,
it is enough to define the following 'operator<' to induce a total
ordering:

bool operator<(compo undindex const& x, compoundindex const& y)
{
if(x.a < y.a) return true;
if(x.a == y.a)
{
if(x.b < y.b) return true;
if(x.b == y.b)
return x.c < y.c;
}
return false;
}

Alternatively, if there is no natural definition for a general-purpose
operator< for the key type in question, it is possible to create a
comparison functor and to instantiate the container with it as
supplementary template parameter.
In either case, the container implementation uses the order to store its
elements in a balanced tree, so that element access becomes possible
with logarithmic complexity regarding to the number of elements currently
in the container.

Hash-based associative containers (guaranteing constant-time access as
long as there are no key collisions) are not (yet) provided by the
standard library but are widely available through third party / extension
libraries.

Falk
Jul 23 '05 #425
In article <wc************ *@shell01.TheWo rld.com>, Robert A Duff <bo*****@shell0 1.TheWorld.com> writes:
Ki*******@SpamC op.net (Larry Kilgallen) writes:
In article <wc************ *@shell01.TheWo rld.com>, Robert A Duff <bo*****@shell0 1.TheWorld.com> writes:
> Ki*******@SpamC op.net (Larry Kilgallen) writes:
>
>> Even Bliss has nested functions. What Ada has that Pascal has in addition
>> to nested functions is uplevel addressing, allowing an inner function to
>> access data declared in outer scopes.
>
> Heh? Pascal has that.


Yes, I just said Pascal had that.


Oh, sorry, I must have thought you said "...in addition to what Pascal
has..." or something. My bad.

Anyway, nested functions are really nested in any useful sense if you
can't do uplevel addressing, right?


I much prefer that capability, but in Bliss (which lacks uplevel addressing)
there is some benefit provided by just the reduced visibility of the
nested function. I know for sure that I can call my nested function
RETRIEVE_STATUS without worrying about a conflict with something else
of the first name.

This is _certainly_ not a Bliss vs. Ada argument, but for me it is
a Bliss vs. C argument.

Please remember, however, that I only mentioned it because you asked :-)
Jul 23 '05 #426
In article <opsnlu5k0q5afh vo@localhost>, "Ed Falis" <fa***@verizon. net> writes:
On 13 Mar 2005 19:31:47 -0500, Robert A Duff
<bo*****@shell0 1.TheWorld.com> wrote:
As far as I know, all Ada compiler vendors take it seriously, and ensure
that their compilers pass 100% of the test suite. My company (Sofcheck,
Inc.) runs the ACATS every night, so we notice any regressions quickly.
We don't normally release a compiler that doesn't pass. I believe
AdaCore (the folks who produce the gcc version of Ada (GNAT)) do the
same.


Yes, we do.
As I said elsewhere, passing 100% of the ACATS, or 100% of any other
test suite, does not guarantee the absense of bugs (obviously).


Agree wholeheartedly.


But it is reassuring to know that a particular compiler does at least
some things correctly.
Jul 23 '05 #427
In article <pa************ *************** *@linuxchip.dem on.co.uk.uk.uk> , "Dr. Adrian Wrigley" <am**@linuxchip .demon.co.uk.uk .uk> writes:
On Sat, 12 Mar 2005 12:59:31 -0500, CTips wrote:
Robert A Duff wrote:
Ki*******@SpamC op.net (Larry Kilgallen) writes:
Even Bliss has nested functions. What Ada has that Pascal has in addition
to nested functions is uplevel addressing, allowing an inner function to
access data declared in outer scopes.
Heh? Pascal has that. In fact, practically every programming language
outside the C family has this feature. It's quite useful -- almost
essential in multi-threaded programs.

- Bob
Yeah, and don't ask what it costs you. I'd carefully forgotten about all
the grungy details about displays and static/dynamic chaining, and you
had to remind me. I particularily like solutions that reserves a
register for the top-of-display/top-of-static-chain. Thats right - blow
away a register for that. And then of course the cost of
maintaining/walking those structures.

If you need thread-private storage, there are *much* cheaper solutions.


But what if you actually need uplevel addressing ?

Why would one presume that users of uplevel addressing actually need
thread-private storage ? Thread-private storage would give just
a single instance per thread, not multiple nested instances per
thread.
isn't uplevel addressing usually zero cost? Are you saying it is
expensive whenever you use it? Or expensive on all programs, whether
or not it is used? Is it absent from C++ because of cost?

Jul 23 '05 #428
On Mon, 14 Mar 2005 17:06:10 +0100, Falk Tannhäuser wrote:
Alternatively, if there is no natural definition for a general-purpose
operator< for the key type in question, it is possible to create a
comparison functor and to instantiate the container with it as
supplementary template parameter.


the problem as I suggested in my previous post is that you have to
pass in the comparison operator or hash function down the tree
of template/generic instantiation, (composing them along the way)
if you want to use maps/hashes. If the necessary functions are
not passed in and built up, you can't instantiate the map/hash.
This means that you can't write an implementation of a template/generic
function using a map/hash without changing some/all the implementations
and interfaces up the instantiation tree (am I right?). Obviously this
may not be possible, for example in large a multi-team or multi
language project. If you changed from a map to a hash, you'd have
to change all the interfaces to pass the hashing function instead
or as well as the comparison operator.

In Ada, a private (nonlimited) generic formal type guarantees
an equality operation and assignment is defined for the type.
These are the only logical requirements for an associative array.
I guess an efficient hash/map library could be written for
private nonlimited generic formals, but neither std::map nor
hashed maps meet this less restrictive requirement.

Put another way, neither language has minimally restrictive
associative arrays in their (or their standard library) specification.
--
Adrian

Jul 23 '05 #429
jtg
Turamnvia Suouriviaskimat ta wrote:
I 'm following various posting in "comp.lang. ada, comp.lang.c++ ,
comp.realtime, comp.software-eng" groups regarding selection of a
programming language of C, C++ or Ada for safety critical real-time
applications. The majority of expert/people recommend Ada for safety
critical real-time applications. I've many years of experience in C/C++ (and
Delphi) but no Ada knowledge.

May I ask if it is too difficult to move from C/C++ to Ada?
What is the best way of learning Ada for a C/C++ programmer?


I had the same problem several years ago.
I was learning Ada from tutorials (www.adahome.com) and online
articles. They were nice and I could get the ideas quickly, but
they were rather shallow and I just could not find answers to
many questions.

After buying one poor book (just the only I could find in local
bookstores) I decided to search for a good book in online bookstores.
I chose "Ada as a second language" by Norman Cohen and I am very glad
with it. All the questions that bothered me (some for months) were
answered in no time. AFAIR always when I needed to know something
about Ada I could find it in this book (well, one exception:
I could not find any information about preprocessor, but some
Ada programmers claim you don't need preprocessor in Ada).

As the title suggests, the book assumes that the reader is already
a skilled programmer :-)
In the book there are many comparisons to C, C++ and some other
languages. If an Ada feature is similar to something in C or C++,
the book mentions it to help pick the idea. If there are differences,
the book mentions them too, to help avoid potential pitfalls
or correct "bad habits", i.e. ways which are natural in one
language but are not the right way to do sth in Ada. So I think
the book would be as useful for you as it was for me.

Is it difficult to move from C/C++ to Ada? Maybe I can tell
you what you may expect (from my own experience only).
When you write in Ada, it is much, much easier to get bugless
program once you manage to compile it (of course if you make
use of Ada features). However, trying to compile an Ada code may be
sometimes very, very frustrating, when the compiler complains
about sth, you look into the code and cannot find out what a strange
rule you are breaking this time in this particular place.

And in Ada you must write a lot more.
You must declare everything, you must be explicit, you must obey
the rules which sometimes seem stupid.
And you must forget about these handy i++, --j, i+=a etc.

Jul 23 '05 #430

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2364
by: Mediocre Person | last post by:
Well, after years of teaching grade 12 students c++, I've decided to make a switch to Python. Why? * interactive mode for learning * less fussing with edit - compile - link - run - debug - edit - compile - link - run -..... * lots of modules * I was getting tired of teaching c++! Bored teacher = bad instruction.
14
1828
by: Gabriel Zachmann | last post by:
This post is not strictly Python-specific, still I would like to learn other university teachers' opinion. Currently, I'm teaching "introduction to OO programming" at the undergrad level. My syllabus this semester consists of a bit of Python (as an example of a scripting language) and C++ (as an example of a compiled language). With C++, I go all the way up to meta-programming. My question now is: do you think I should switch over to...
3
1536
by: andy_irl | last post by:
Hi there I have been asked to teach HTML to a group in our local village community. It is nothing too serious, just a community development grant aided scheme. It will be a 10 week course of two hours per week and will mainly consist of mature students. I may or may not include GUI's depending if I can fit it all in to the time allocated. I was wondering if anyone could point me to any useful teaching resources for HTML on the web ie...
12
2001
by: Pierre Senellart | last post by:
I am going to teach a basic Web design course (fundamentals of HTML/CSS, plus some basic client-side (JavaScript) and server-side (PHP, perhaps XSLT) scripting). Most of the students do not have any previous knowledge of all of this. I am strongly considering teaching XHTML 1.0 Strict instead of HTML 4.01 strict, for the following reasons: - XML syntax is far more simple to teach than HTML/SGML, simply because there are not as many...
16
4379
by: msnews.microsoft.com | last post by:
I am teaching C# to my 11 year old child. One challenge is that all the C# books I own and that I have seen in bookstores are full of language that is not easily comprehended by a student at that age. Can anyone recommend books (or perhaps websites) tuned for younger audiences? BTW, its amazing how fast a student can absorb this kind of information at that age. Lucky them! Thanks, Bruce
24
2865
by: Richard Aubin | last post by:
I'm really new to vb.net programming and programming in general. I would like to teach myself on how to program effectively and I have the financial and time resources to do so. Can I anyone recommend and point me in the right direction where I should start? -- Richard Aubin
0
1715
by: e.expelliarmus | last post by:
check this out buddies. kool website for: * hacking and anti hacking tricks * anti hackng tricks. * registry tweaks * orkut tricks * small virus * computer tricks and loads of different tricks... www.realm-of-tricks.blogspot.com www.registrydecoded.blogspot.com
1
3896
by: JosAH | last post by:
Greetings, Introduction This week's tip describes a few old tricks that are almost forgotten by most people around here. Sometimes there's no need for these tricks anymore because processors nowadays are so fast and memory comes in abundance. But still, if we implement an algorithm that is better, or more efficient, than another one, those faster processors run the first algorithm faster than the other one. If an algorithm takes less...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.