Making C better (by borrowing from C++)

M

masood.iqbal

I know that this topic may inflame the "C language Taleban", but is
there any prospect of some of the neat features of C++ getting
incorporated in C? No I am not talking out the OO stuff. I am talking
about the non-OO stuff, that seems to be handled much more elegantly in
C++, as compared to C. For example new & delete, references, consts,
declaring variables just before use etc.

I am asking this question with a vested interest. I would really like
to use these features in my C programs.

Masood
 
I

infobahn

I know that this topic may inflame the "C language Taleban", but is
there any prospect of some of the neat features of C++ getting
incorporated in C? No I am not talking out the OO stuff. I am talking
about the non-OO stuff, that seems to be handled much more elegantly in
C++, as compared to C. For example new & delete,

Pointless without adding constructors and destructors too, which
might be further than the C community wants to go.
references,

An excellent way to confuse people still further wrt pointers.

Got them.
declaring variables just before use etc.

Got them in C99.
 
C

Chris Croughton

I know that this topic may inflame the "C language Taleban", but is
there any prospect of some of the neat features of C++ getting
incorporated in C? No I am not talking out the OO stuff. I am talking
about the non-OO stuff, that seems to be handled much more elegantly in
C++, as compared to C. For example new & delete, references, consts,
declaring variables just before use etc.

Note that quite a few features have already made it back -- void,
declaring variables in the middle of code, single-line comments for
instance.

There is no point to new and delete without constructors and
destructors, and to have those you'd need to introduce classes (and then
you'd have "C with classes", which was how C++ started out.

References are nice in some ways, but are really just syntactic sugar
round pointers, and can be even more confusing (especially when it isn't
obvious that a parameter may change).

Proper named and typed constants would indeed be useful, C's version of
const is a compiler convenience, and I can see those making it into C at
some point, but most of the other features depend on classes.

I can think of other things I'd like more. A typeof operator, for
example, and to have known-width arithmetic types build into the
language instead of via a header file (which isn't guaranteed to even
provide them). I'd really like a portable way of having enum values as
strings so that I could use strtoenum() for input and a printf
descriptor to output an enum as its named values. I'd like those in C++
as well...
I am asking this question with a vested interest. I would really like
to use these features in my C programs.

Why not use C++ instead, if that's what you like? That's what I do, I
use C++ for things where I want high-level features and I use C for
portability (adding C++ features to C won't make them portable, we're
over 5 years after the last C standard came out and I still can't safely
assume that any of the features introduced are portable (there seems to
be possibly one compiler and library which is fully C99 compliant) and
some systems don't even fully implement the standard from 10 years
before that).

Chris C
 
C

CBFalconer

Chris said:
.... snip ...

References are nice in some ways, but are really just syntactic
sugar round pointers, and can be even more confusing (especially
when it isn't obvious that a parameter may change).

I used to miss them sorely, but eventually got used to the idea
that the caller can tell when it is exposing its internal data to
outside influence without examining the header of the called
function. Of course the cost is that pointers are not under
control, and that proper verification is impossible to all
practical purposes.
 
M

Mark McIntyre

I know that this topic may inflame the "C language Taleban", but is
there any prospect of some of the neat features of C++ getting
incorporated in C?

Some C++ features made it into C99. Others don't make sense in C, such as
new, delete, reference variables and so forth. For your info consts have
been in C for decades, and declare-before-use is in C99.
I am asking this question with a vested interest. I would really like
to use these features in my C programs.

Then use C++
 
J

jacob navia

I know that this topic may inflame the "C language Taleban", but is
there any prospect of some of the neat features of C++ getting
incorporated in C? No I am not talking out the OO stuff. I am talking
about the non-OO stuff, that seems to be handled much more elegantly in
C++, as compared to C. For example new & delete, references, consts,
declaring variables just before use etc.

I am asking this question with a vested interest. I would really like
to use these features in my C programs.

Masood

The lcc-win32 compiler adds some features of C++ like:

o operator overloading
o default arguments to functions
o references

Declaring variables just vefore use is part of the
C99 standard

http://www.cs.virginia.edu/~lcc-win32
 
M

Mark McIntyre

I know that this topic may inflame the "C language Taleban", but is
there any prospect of some of the neat features of C++ getting
incorporated in C?

Some C++ features made it into C99. Others don't make sense in C, such as
new, delete, reference variables and so forth. For your info consts have
been in C for decades, and declare-before-use is in C99.
I am asking this question with a vested interest. I would really like
to use these features in my C programs.

Then use C++
 
C

Chris Croughton

I used to miss them sorely, but eventually got used to the idea
that the caller can tell when it is exposing its internal data to
outside influence without examining the header of the called
function. Of course the cost is that pointers are not under
control, and that proper verification is impossible to all
practical purposes.

When I'm writing C++ I use reference parameters, because I like the
cleanness of the call. However I've been bitten by them several times,
so it's a kind of "love-hate" relationship, I can see good reasons both
for using them and for avoiding them. The thing I do like and miss from
C++ (which C99 has now but none of the compilers for which I have to
write portable C code supports) is declaring 'local' loop variables:

for (int i = 0; i < n; ++i)
...

I'm always forgetting that C89 doesn't have that...

Chris C
 
K

Keith Thompson

I know that this topic may inflame the "C language Taleban", but is
there any prospect of some of the neat features of C++ getting
incorporated in C? No I am not talking out the OO stuff. I am talking
about the non-OO stuff, that seems to be handled much more elegantly in
C++, as compared to C. For example new & delete, references, consts,
declaring variables just before use etc.

As others have pointed out, some of these things are already in C99.
And I believe that prototypes were first introduced in (pre-standard)
C++ and added to C with the 1989 ANSI standard. I don't think there's
any great resistance to the idea of adding some C++ features to C *if*
they fit into the C language.

On the other hand, if you want C++ features that C doesn't have, the
easiest way to get them is to use C++.

Incidentally, calling people "Taleban" is a really bad way to
influence them.
 
R

Randy Howard

I know that this topic may inflame the "C language Taleban", but is
there any prospect of some of the neat features of C++ getting
incorporated in C? No I am not talking out the OO stuff.

What would be the point? There is nothing precluding you from using
C++, without the "OO stuff", to write whatever it is you are interested
in that you can't do the way you want it done in pure C.
I am talking about the non-OO stuff, that seems to be handled much
more elegantly in C++, as compared to C.

C++ is over there --------->
I am asking this question with a vested interest. I would really like
to use these features in my C programs.

Rename your C program file extensions to .cpp, find every occurrence
of code in those programs that uses something in a different way than
it is in C++, then add those features that you like from C++.

The proper place to discuss this is in a newsgroup with "c++" in the
title.
 
R

Randy Howard

The lcc-win32 compiler adds some features of C++ like:

And using those features means you are writing in some other
language that is not C, call it "Navia" perhaps?
 
W

websnarf

I know that this topic may inflame the "C language Taleban", but is
there any prospect of some of the neat features of C++ getting
incorporated in C? No I am not talking out the OO stuff. I am
talking about the non-OO stuff, that seems to be handled much more
elegantly in C++, as compared to C. For example new & delete,

new & delete are really tied to to the semantics of constructor and
destructors in C++. There is little point in adding them to C without
adding the whole "class" paradigm.
[...] references,

Yes, I absolutely agree with this. References allow a receiving
function to better rely on the fact that the variable is supposed to be
backed by valid memory -- specifically it gives a syntactically sound
way of passing "pointers" that have better chances of being good at
compile time (its still possible to pass an array element that's beyond
the array's boundaries -- but at least you know you can't pass a
pointer to the heap that has been freed, or NULL, or just pure
garbage). As it is today, C programmers do this with raw pointers
which increases the potential danger by allowing for semantics that
usually are not intended.
[...] consts,

C has const. I don't quite see how C++'s consts are not redundant with
either #define or enums. You can even make enums locally scoped.
declaring variables just before use etc.

This is in C99, and personally I would rather have seen this *REMOVED*
from C++, than added to C. It makes the job of reading code twice as
hard -- if we wanted that, we might as well be using Perl. If you want
to do "just in time constructors", wrap your code with additional
scope.
I am asking this question with a vested interest. I would really
like to use these features in my C programs.

Personally, I think namespace should be added to that list. The single
shared global namespace situation that exists in C today is nothing
short of haphazard.
 
K

Keith Thompson

new & delete are really tied to to the semantics of constructor and
destructors in C++. There is little point in adding them to C without
adding the whole "class" paradigm.

They're tied to constructors and destructors in C++, but they needn't
be in C. They could be added to the language as built-in versions of
malloc() and free(). For example:

int *ptr = new int;
...
delete ptr;

is valid C++, and has nothing to do with constructors or destructors.

Since C already has malloc() and free(), new and delete wouldn't add
much value, but if I were designing a C-like language from scratch I'd
probably use new and delete *instead of* malloc() and free().

[...]
Personally, I think namespace should be added to that list. The single
shared global namespace situation that exists in C today is nothing
short of haphazard.

Agreed.
 
I

infobahn

Keith said:
Since C already has malloc() and free(), new and delete wouldn't add
much value, but if I were designing a C-like language from scratch I'd
probably use new and delete *instead of* malloc() and free().

How would you deal with resizing? old?
 
C

Chris Croughton

C has const. I don't quite see how C++'s consts are not redundant with
either #define or enums. You can even make enums locally scoped.

Try declaring an enum with floating point type. Or using a const int as
a case value. I think that C++ should have found something else to call
them (and both C and C++ should have stopped the overloading of the
'static' keyword, C++ even adds yet another meaning to it), but typed
constants are useful and don't have the pitfalls of macros.
This is in C99, and personally I would rather have seen this *REMOVED*
from C++, than added to C. It makes the job of reading code twice as
hard -- if we wanted that, we might as well be using Perl. If you want
to do "just in time constructors", wrap your code with additional
scope.

Used properly (as in declaring loop variables only when they are used)
it makes it more readable, not less. I've seen a lot of errors where
someone has used a variable (often i or j) for a loop, it has compiled
(it was already declared) but it has done unexpected things because it
wasn't obvious that the variable was already in use.
Personally, I think namespace should be added to that list. The single
shared global namespace situation that exists in C today is nothing
short of haphazard.

Definitely. Then we could get rid of "static used for
non-external-scope" and leave it for "variable does not change between
invocations of the function". As well as getting rid of prefixes just
in case something else has a similarly names function.

Chris C
 
J

jacob navia

new & delete are really tied to to the semantics of constructor and
destructors in C++. There is little point in adding them to C without
adding the whole "class" paradigm.

I agree. there is not a lot to be gained with "new" and "delete"
without going into constructors.
[...] references,


Yes, I absolutely agree with this. References allow a receiving
function to better rely on the fact that the variable is supposed to be
backed by valid memory -- specifically it gives a syntactically sound
way of passing "pointers" that have better chances of being good at
compile time (its still possible to pass an array element that's beyond
the array's boundaries -- but at least you know you can't pass a
pointer to the heap that has been freed, or NULL, or just pure
garbage). As it is today, C programmers do this with raw pointers
which increases the potential danger by allowing for semantics that
usually are not intended.

Again, I agree. The lcc-win32 compiler implements them, and they do
improve program security.
[...] consts,


C has const. I don't quite see how C++'s consts are not redundant with
either #define or enums. You can even make enums locally scoped.

declaring variables just before use etc.


This is in C99, and personally I would rather have seen this *REMOVED*
from C++, than added to C. It makes the job of reading code twice as
hard -- if we wanted that, we might as well be using Perl. If you want
to do "just in time constructors", wrap your code with additional
scope.

I am asking this question with a vested interest. I would really
like to use these features in my C programs.

Use the lcc-win32 C compiler:
http://www.cs.virginia.edu/~lcc-win32
 
J

jacob navia

Randy said:
And using those features means you are writing in some other
language that is not C, call it "Navia" perhaps?
The C99 standard explicitly allows extensions, and those extensions have
been designed keeping compatible with the C99 standard.

There are NO NEW keywords. You can still say:

int operator = 45;

int operator(int g);

etc.

Each implementation of the C standard can extend the language
in the form prescribed by the standard, and *most* of them
do.

Your answer is typical for people that say that C should stay
at the level of assembly language and only C++ should have any
higher level constructs.

Operator overloading is a standard extension that is used in Fortran,
for instance, and in many other languages.
 
R

Randy Howard

The C99 standard explicitly allows extensions, and those extensions have
been designed keeping compatible with the C99 standard.

And what happens when you want to port that source code to a platform
that doesn't have a compiler with your extensions?
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top