C vs. C++

J

James Kuyper

jacob said:
max(a,b)
min(a,b)

As pointed out elsethread, this needs a special template
and generic functions in C++. Imagine.

In C a simple macro is enough.

Whatever macro definition you're thinking of, it will probably work just
as well in C++; if you do something unnecessarily complicated, it's
possible but unlikely that it might need a re-write.

A generic function template is used in C++, not because it's necessary
to achieve the same functionality as the C macro you are thinking of,
but because the template allows different functionality. It provides
greater type safety and makes it trivial to avoid double-evaluation of
the arguments. The use of a function template also enables definition of
the overloaded version of max(), which uses a user-specified function
object to perform the comparison, rather than using '<'.
 
R

Richard Bos

Richard said:
The simple and clear question is:

,----
| >> What simple program can you program in C that you can not as simply
| >> program in C++?
`----

The problem with that question, simple and clear as it is, is that you
might as well ask

What simple program can you program in C that you can not as simply
program in BASIC?

or

What book can you write in English that you can not as simply write in
Dutch?

And yet, I don't want to read translations of Milton, but the original.

Some questions simply are _too_ simple.

Richard
 
K

Kaz Kylheku

Yes according to Stoustrup

the two comments below do NOT say C++ is a superset of C. Read them
carefully.

Take a look at Message-ID: <[email protected]>,
for instance through Google Groups:

http://groups.google.ca/group/comp.lang.c/msg/a13484bb83cd9f2b?dmode=source
S

Now who made that nice diagram?

Note that C99 isn't a superset of C90 either.

Maybe this strict superset concept isn't terribly useful for
language comparison.

The concept doesn't even prevent one language from being promoted
as a new revision of another!

It's most useful for winning a bet at engineering district's watering
hole, for a free beer. The second use is that it helps you to know
what you are doing when converting programs from one dialect to
another. (Bjarne also claims that converting a large program from
C to C++ can be done in a reasonably short time).
He says anything you can write in C he things you can write
better in C++.

He does not. These two comments says that anything you can write in C, you can
write equally well in C++, not better.

Now who is it who should read them carefully?
And from his own web site
http://www.research.att.com/~bs/bs_faq.html#C-is-subset

Is C a subset of C++?
In the strict mathematical sense, C isn't a subset of C++.

Here Bjarne does not deny, and even insinuates, that there is some
sense in which C /can/ be regarded as a subset of C++. We just won't
find that sense by in the area of formal languages.

I can identify that sense: it's the semantic and expressive sense.
There are
programs that are valid C but not valid C++ and even a few ways of
writing code that has a different meaning in C and C++.

Same with C99 versus C90. So C++ can be regarded as a revision of C90 in
the same way as C99 is regarded as revision of C90.
 
K

Kaz Kylheku

max(a,b)
min(a,b)

As pointed out elsethread, this needs a special template
and generic functions in C++. Imagine.

The semantics of max is that it's generic, because it has to support
various type combinations like int x double -> double, et cetera.
So it comes as no surprise that it should be implemented using
generic functions.

Mark Wooding's template solution for the return type is brilliant, and easy to
use. Just make your max template take two types S and T for the arguments, and
In C a simple macro is enough.

The macro is a bit too simple.

It evaluates its arguments multiple times.

You cannot take the address of the macro because there is no underlying
function, making it impossible to use as a higher order function.

The macro is also based on polymorphism, namely generic operators, which do all
the work. The < operator compares any two numeric types, converting one of
them to the type of the other as necessary, and the ternary operator selects
between two expressions, converting the result to a type based on the type of
those two according to hard-coded rules, which you cannot customize or extend
to your own types.

Exactly the same macro solution can be expressed in C++, yet programmers
sometimes want to reach for something more.
 
K

Keith Thompson

jacob navia said:
max(a,b)
min(a,b)

As pointed out elsethread, this needs a special template
and generic functions in C++. Imagine.

In C a simple macro is enough.

In C++, you can define and use exactly the same macros, so that's
hardly an argument in favor of the point.
 
T

Tony

Bartc said:
Is it possible to separate out Templates from C++ (perhaps losing some
features) and make them available to another language, in the same way as
the C preprocessor can be?

(I'm assuming here that Templates are just a sophisticated kind of macro.)

The first implementation of templates indeed was macro-based. Search around
for a generic.h file in your compiler's runtime library code which
facilitated the macro-based template techniqes of the time, or on the net if
that header has been deprecated. So, the answer to your question is yes,
noting that you asked if today's monstrous template machinery's "features"
could be "dumbed down" a bit if need be.

The problem (and indeed is a problem) with adding features (and templates
are an example) to a language/compiler is that there is compulsion to build
in "everything including the kitchen sink" and even evolve that into greater
complexity over time. It does effectively lock out or hinder many potential
compiler and tool developers and language designers. Adding a
language/compiler feature should be handled with much restraint and seek the
proverbial "80% solution" rather than being anal about it. This paragraph
may indeed describe C++'s nemesis.

Tony
 
R

Richard Bos

Lew Pitcher said:
To keep it even simpler, I have seen compilers for C, Fortran,
Pascal (and others) that generate assembly language. Thus all
those languages are just syntactic sugar around assembly language.

And, taken to the limit, /all/ compiled computer languages are just
syntactic sugar around machine language. ;-) [/QUOTE]

Machine language is syntactic sugar around shoving electrons about.

Richard
 
R

Richard Bos

Keith Thompson said:
Oh, you had electrons? When I was your age, ...

I always did suspect you are older than I am, Keith, but I didn't know
you go back to less than a second after the Big Bang...

Richard
 
D

David Thompson

You mean the C std library. C++ has different rules. The problem
is that this thread is discussing C++ in a C newsgroup.

min (and max) is NOT in the C standard library. In fact these are for
my money probably the single most annoying omission.

They are in C++, but as said elsethread only in std:: namespace, so
they don't (necessarily) conflict with userdefined ones.
 

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
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top