generic programming is very powerful. c should support it

S

steve yee

i think c should adapt c++ template standard, as well as namespace. if
so, c can replace c++ in many cases.
 
T

Tom St Denis

steve said:
i think c should adapt c++ template standard, as well as namespace. if
so, c can replace c++ in many cases.

I don't get this comment. You want C to become C++ so it could replace
C++? It WOULD BE C++.

What do you like about C that isn't in C++?

Tom
 
S

steve yee

jacob said:
Simplicity.

No automatic constructors, no OO orientation.

C has a lot of advantages.

yes. what i mean is that c can adapt template and namespace from c++,
because it's very useful and does not hurt the simplicity of c. this
will not make c become c++, because there's much more than generic and
namespace in c++'s feature.
 
J

Julienne Walker

steve said:
yes. what i mean is that c can adapt template and namespace from c++,
because it's very useful and does not hurt the simplicity of c. this
will not make c become c++, because there's much more than generic and
namespace in c++'s feature.

Adding a turing complete sub-language won't hurt the simplicity of C?
 
C

Christopher Benson-Manica

steve yee said:
i think c should adapt c++ template standard, as well as namespace. if

How do you propose to make use of templates without introducing the
notion of a class?
 
?

=?ISO-8859-1?Q?St=E9phane_Zuckerman?=

yes. what i mean is that c can adapt template and namespace from c++,
because it's very useful and does not hurt the simplicity of c. this
will not make c become c++, because there's much more than generic and
namespace in c++'s feature.

I don't get it. If you want templates and namespaces, why don't you use a
subset of C++ (namely, C++ without the OO approach) ? It'd be some kind of
subset of C with additional things (such as ... templates and namespaces
for instance).
 
J

Julienne Walker

Christopher said:
How do you propose to make use of templates without introducing the
notion of a class?

Templates aren't restricted to classes, though their usefulness
decreases considerably when all you have are function templates. In
theory, you could apply a template to a structure, though that strikes
me as awkward in the absence of member functions. Of course, there are
times when I've yearned for member functions in C. Maybe the OP is
looking for a watered down class concept too. ;-)
 
T

Thomas J. Gritzan

steve said:
i think c should adapt c++ template standard, as well as namespace. if
so, c can replace c++ in many cases.

There is no function overloading in C. C++ templates without that would
not be useful.

Why don't you just use C++, since C++ has adopted most of the standard
library of C?
 
E

Eric Sosman

steve yee wrote On 07/26/06 09:01,:
i think c should adapt c++ template standard, as well as namespace. if
so, c can replace c++ in many cases.

Make a formal proposal to the Committee. Be sure to
let us know how you get on with them, will you? 'bye, now!
 
S

Serve Laurijssen

Julienne Walker said:
Adding a turing complete sub-language won't hurt the simplicity of C?

hahaha indeed, templates are very very powerful but only if you would add a
pile of other c++ features too. function/operator overloading, member
functions, default arguments etc. You'll basically end up with C++ so why
not just use that then!
 
W

websnarf

steve said:
i think c should adapt c++ template standard, as well as namespace. if
so, c can replace c++ in many cases.

namespaces I agree with. In fact, its one of those obvious
self-contained features from C++ that isn't really OO related and
solves a real problem that exists in C. (Unlike arbitrarily locatable
declarations, which exists in C++ specifically to solve a problem with
constructors.) References are another feature that would be nice to
take from C++ that is similarly self-contained and not really OO
related.

But C++'s templates operate on classes, and really kind of needs to use
them, so I don't know how you could do that without making C really
close to C++. However, I do agree that generic programming is
extremely powerful, and should be supported. The good news is that, to
a large extent, it *IS* supported via the preprocessor.

For example, see:

http://www.pobox.com/~qed/ll.zip

and

http://www.pobox.com/~qed/gstack.zip

These are general stacks and linked lists, in which you can at compile
time simply select the base data type. The macros emit a bunch of
functions which interface to a void * based back end. This allows the
interface to be type-safe, while using the unsafe void * type to unify
the back-end code (to be space efficient.) C++ only has the type-safe,
code replication semantics available to it. On the other hand C++'s
automatic constructor/destructor semantics help a lot -- with the two C
libraries above, it is assumed that you deal with allocation and
deallocation of the elements yourself.

So, they are somewhat complicated to set up in C, but it is certainly
possible. And I recommend doing so whenever it makes sense.
 
I

Ian Collins

But C++'s templates operate on classes, and really kind of needs to use
them, so I don't know how you could do that without making C really
close to C++.

There are function templates as well as class templates.
 
W

Walter Roberson

namespaces I agree with. In fact, its one of those obvious
self-contained features from C++ that isn't really OO related and
solves a real problem that exists in C. (Unlike arbitrarily locatable
declarations, which exists in C++ specifically to solve a problem with
constructors.)

Although arbitrarily locatable declarations may not solve a semantic
problem in C, as a programmer I like to have them.

It is fairly common to not need variables until relatively far into a
function; it is useful to be able to hold off declaration of the
variables until a convenient logical point. It makes coding simpler
than having to go back to the beginning to add in a variable, and it
leaves the type information close to where the information is required
instead of having to search back for it.

One can simulate them in C by using blocks and declaring the variable
at the beginning of the block, but that pushes the indentation level
further in.
 
I

Ian Collins

Walter said:
Although arbitrarily locatable declarations may not solve a semantic
problem in C, as a programmer I like to have them.

It is fairly common to not need variables until relatively far into a
function; it is useful to be able to hold off declaration of the
variables until a convenient logical point. It makes coding simpler
than having to go back to the beginning to add in a variable, and it
leaves the type information close to where the information is required
instead of having to search back for it.

One can simulate them in C by using blocks and declaring the variable
at the beginning of the block, but that pushes the indentation level
further in.

You have them with C99.
 
K

Keith Thompson

steve yee said:
i think c should adapt c++ template standard, as well as namespace. if
so, c can replace c++ in many cases.

First rule of language design: You can't change just one thing.

(Ok, I don't know if that's really considered the "first rule".)

I can see that introducing C++-style namespaces might be a reasonable
thing to do, that wouldn't necessarily do too violence to the rest of
the language or to existing code. (I could be missing something,
though; I haven't taken the time to think it through.)

Templates, however, seem more complex, and probably more tied into the
rest of C++ (specifically to the portions of C++ outside the C-like
subset).

Remember that C++ started as "C with classes". What you're proposing
is "C with templates" (except that you want this to become "C" some
day).

If you're serious about this, you'll need to demonstrate existing
practice.

The simplest approach is to write a body of code in C++, but
restricting yourself to the C-like subset of C++ plus templates.
(Programming in the intersection of C and C++ is rarely a good idea,
but it would be useful here.) If you can demonstrate that this has
substantial benefits over plain C, you may have something.

You might consider implementing a C++ filter that will flag anything
outside your dialect. For example, it would reject anything using
classes. This filter combined with a C++ compiler would be a compiler
for your "C with templates" dialect.

Another approach would be to implement a preprocessor, similar to
Stroustrup's cfront, that would translate your dialect into conforming C.

Of course, even if no standard committee ever expresses any interest
in this, there's nothing stopping you from using it yourself, or
making it available to others.

Language changes start with somebody having a good idea, but they
never end there. (Or rather, they usually do end there, and go no
further.) There's a lot of work to do after that; you'll need either
to do it yourself, or to persuade someone else to do it. I can just
about guarantee that the C standard committee isn't going to say, "Oh
good, let's add templates!" without a lot of persuasion.
 
D

Dave Vandervies

How do you propose to make use of templates without introducing the
notion of a class?

By writing type-generic code once and using it type-safely for different
types, which works just as well for functions as it does for classes.

If you make the template parameters part of the function's name, you
don't even have to introduce overloading, though that does require the
template parameters at every use of the template instead of having the
template function be an overloaded function named by the template name.

--------
/*Generic template for scalar types*/
template<typename T>
int less<T>(const T *left, const T *right)
{ return *left < *right; }

/*Specialization of less<>() for struct foo*/
int less<struct foo>(const struct foo *left,const struct foo *right)
{ return (left->A < right->A) || (left->B < right->B); }

/*Templated sort function
(with default parameter, but that's not a strictly necessary feature)
*/
template<typename T,int (*pred)(const T *,const T *)=less<T> >
void bogosort<T,pred>(T *data, size_t length)
{
for(;;)
{
int sorted=1;
int i;

for(i=0;i<length-1;i++)
if(pred<T>(data+i+1,data+i))
sorted=0;
if(sorted)
return;

shuffle<T>(data,length);
}
}

void sort_contents(struct three_arrays *args)
{
/*sort an array of int in increasing order*/
bogosort<int>(args->int_array,args->int_size);

/*sort an array of double in arbitrary order*/
bogosort<double,my_ordering>(args->double_array,args->double_size);

/*Sort an array of struct foo in increasing order*/
bogosort<struct foo>(args->foo_array,args->foo_size);
}
--------

If I'm not mistaken, a C++-style template syntax doesn't even give
a different interpretation to anything that's not a syntax error in C
(though you'd have to choose between introducing new keywords and having
to call it _Template - but we could probably use static for typename).


dave
 
I

Ian Collins

Dave said:
By writing type-generic code once and using it type-safely for different
types, which works just as well for functions as it does for classes.

If you make the template parameters part of the function's name, you
don't even have to introduce overloading, though that does require the
template parameters at every use of the template instead of having the
template function be an overloaded function named by the template name.

--------
/*Generic template for scalar types*/
template<typename T>
int less<T>(const T *left, const T *right)
{ return *left < *right; }
Templates may work fine for functions, but you open the function
overload/name mangling can of worms. You would also end up with all of
the messy name resolution rules from C++.
 
G

goose

Christopher said:
How do you propose to make use of templates without introducing the
notion of a class?


<OT>

I suppose plain 'ol generic functions? In the presence of function
overloading I'd expect something like this

header:
void foo (<X> arg1, <Y> arg2);

implementation:
void foo (int arg1, float arg2) { ... }
void foo (int arg1, int arg2) { ... }
void foo (struct mystruct arg1, int arg2) { ... }


Although it'd be a pain to write a function
definition for all the types of arguments that
the generic function "foo" can be called with.


goose,
 
K

Keith Thompson

goose said:
<OT>

I suppose plain 'ol generic functions? In the presence of function
overloading I'd expect something like this

header:
void foo (<X> arg1, <Y> arg2);

implementation:
void foo (int arg1, float arg2) { ... }
void foo (int arg1, int arg2) { ... }
void foo (struct mystruct arg1, int arg2) { ... }


Although it'd be a pain to write a function
definition for all the types of arguments that
the generic function "foo" can be called with.

That doesn't look like a template; it looks like function overloading
with an odd syntax. In a C++, you can write a single implementation
and the compiler will expand it for each instance.
 

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top