Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: New release of the Dynace OO extension to C

Reply
Thread Tools

Re: New release of the Dynace OO extension to C

 
 
fft1976
Guest
Posts: n/a
 
      07-15-2009
On Jul 13, 1:10*pm, Blake McBride <(E-Mail Removed)> wrote:

(...)

But OOP can be done in C. Generic programming on the other hand ...

Good luck using macros for that! It's "possible", but my many
inquiries into whether there is a macros-based STL analog in the C
world turned up nothing.
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      07-15-2009
fft1976 wrote:
> But OOP can be done in C.


I think that's debatable.

You can simulate object-oriented programming in C to an extent, but
since the language has basically no support, it will inevitably be
rather "hacky" and complicated.

The gtk+ library for C is a good example of a C library which
extensively uses OO techniques. However, the resulting code is
necessarily uglier and less efficient than the equivalent C++ code would
be. (For example, when using gtk+, every single pointer cast from one
type to another, even when it's from a derived object type to a base
object type, is done dynamically at runtime, with runtime checks.)

> Generic programming on the other hand ...
>
> Good luck using macros for that! It's "possible", but my many
> inquiries into whether there is a macros-based STL analog in the C
> world turned up nothing.


There are many things doable with templates which are impossible to do
in C with precompiler macros. A very trivial example:

//--------------------------------------------------------------
template<typename T>
void foo(T value)
{
std::cout << "The value is: " << T << std::endl;
}
//--------------------------------------------------------------

A slightly more complicated example:

//--------------------------------------------------------------
template<typename T>
void foo()
{
std::cout << "The specified type is"
<< (std::numeric_limits<T>::is_integer ? "" : " not")
<< " an integral type.\nThe maximum value which can "
<< "be represented by it is: "
<< std::numeric_limit<T>::max() << std::endl;
}
//--------------------------------------------------------------
 
Reply With Quote
 
 
 
 
Chris M. Thomasson
Guest
Posts: n/a
 
      07-15-2009
"fft1976" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Jul 13, 1:10 pm, Blake McBride <(E-Mail Removed)> wrote:

> (...)


> But OOP can be done in C. Generic programming on the other hand ...


Well, you can definitely create highly minimalist generic interfaces fairly
easily in C:

http://clc.pastebin.com/f52a443b1




> Good luck using macros for that! It's "possible", but my many
> inquiries into whether there is a macros-based STL analog in the C
> world turned up nothing.


Well, you can also do something crazy like:

http://h30097.www3.hp.com/cplus/6026pro_genr.html

http://www.softwarepreservation.org/...generic.h/view


funny:
__________________________________________________ ____________________
#define CONCAT_RAW(mp_token1, mp_token2) \
mp_token1 ## mp_token2

#define CONCAT(mp_token1, mp_token2) \
CONCAT_RAW(mp_token1, mp_token2)


#define DECLARE_STACK(mp_name, mp_type) \
void CONCAT(mp_name, _stack_push) ( \
mp_type* const self, \
mp_type const node \
);\
mp_type \
CONCAT(mp_name, _stack_pop) ( \
mp_type* const self \
);


#define DEFINE_STACK(mp_name, mp_type, mp_pname) \
void CONCAT(mp_name, _stack_push) ( \
mp_type* const self, \
mp_type const node \
) { \
node->mp_pname = *self; \
*self = node; \
} \
mp_type \
CONCAT(mp_name, _stack_pop) ( \
mp_type* const self \
) { \
mp_type node = *self; \
if (node) *self = node->mp_pname; \
return NULL; \
}




#include <stdlib.h>


DECLARE_STACK(foo, struct foo*)


struct foo {
struct foo* next;
};


DEFINE_STACK(foo, struct foo*, next)


static struct foo* g_stack = NULL;


int main(void) {
foo_stack_push(&g_stack, malloc(sizeof(*g_stack)));
foo_stack_push(&g_stack, malloc(sizeof(*g_stack)));
foo_stack_push(&g_stack, malloc(sizeof(*g_stack)));
foo_stack_push(&g_stack, malloc(sizeof(*g_stack)));
free(foo_stack_pop(&g_stack));
free(foo_stack_pop(&g_stack));
free(foo_stack_pop(&g_stack));
free(foo_stack_pop(&g_stack));
return 0;
}
__________________________________________________ ____________________



Generic type-safe intrusive stack? lol.

 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      07-15-2009

"Juha Nieminen" <(E-Mail Removed)> wrote in message
news:0gm7m.90$w%(E-Mail Removed)...
> fft1976 wrote:
>> But OOP can be done in C.

>
> I think that's debatable.
>
> You can simulate object-oriented programming in C to an extent, but
> since the language has basically no support, it will inevitably be
> rather "hacky" and complicated.
>
> The gtk+ library for C is a good example of a C library which
> extensively uses OO techniques. However, the resulting code is
> necessarily uglier and less efficient than the equivalent C++ code would
> be. (For example, when using gtk+, every single pointer cast from one
> type to another, even when it's from a derived object type to a base
> object type, is done dynamically at runtime, with runtime checks.)
>
>> Generic programming on the other hand ...
>>
>> Good luck using macros for that! It's "possible", but my many
>> inquiries into whether there is a macros-based STL analog in the C
>> world turned up nothing.

>
> There are many things doable with templates which are impossible to do
> in C with precompiler macros. A very trivial example:
>
> //--------------------------------------------------------------
> template<typename T>
> void foo(T value)
> {
> std::cout << "The value is: " << T << std::endl;
> }


You mean << value << here?

--
bart

 
Reply With Quote
 
ld
Guest
Posts: n/a
 
      07-15-2009
On 15 juil, 17:06, Juha Nieminen <(E-Mail Removed)> wrote:
> fft1976 wrote:
> > But OOP can be done in C.

>
> * I think that's debatable.
>
> * You can simulate object-oriented programming in C to an extent, but
> since the language has basically no support, it will inevitably be
> rather "hacky" and complicated.


Wrong.

> * The gtk+ library for C is a good example of a C library which
> extensively uses OO techniques.


This is the worst example I know. Heavy, slow, odd.

> However, the resulting code is
> necessarily uglier and less efficient than the equivalent C++ code would
> be. (For example, when using gtk+, every single pointer cast from one
> type to another, even when it's from a derived object type to a base
> object type, is done dynamically at runtime, with runtime checks.)


This is related to gtk+, not OOP in C.

> > Generic programming on the other hand ...

>
> > Good luck using macros for that! It's "possible", but my many
> > inquiries into whether there is a macros-based STL analog in the C
> > world turned up nothing.

>
> * There are many things doable with templates which are impossible to do
> in C with precompiler macros.


Do you _really_ know what is doable with C macros?

> A very trivial example:
>
> //--------------------------------------------------------------
> template<typename T>
> void foo(T value)
> {
> * * std::cout << "The value is: " << T << std::endl;}
>
> //--------------------------------------------------------------
>
> * A slightly more complicated example:
>
> //--------------------------------------------------------------
> template<typename T>
> void foo()
> {
> * * std::cout << "The specified type is"
> * * * * * * * << (std::numeric_limits<T>::is_integer ? "" : " not")
> * * * * * * * << " an integral type.\nThe maximum value which can "
> * * * * * * * << "be represented by it is: "
> * * * * * * * << std::numeric_limit<T>::max() << std::endl;}
>
> //--------------------------------------------------------------


Polymorphism can replace template here and hence be done in C. A C++
MTP example would be better to show something not possible in C at
compile time. But this not OOP.

a+, ld.

 
Reply With Quote
 
BGB / cr88192
Guest
Posts: n/a
 
      07-15-2009

"Juha Nieminen" <(E-Mail Removed)> wrote in message
news:0gm7m.90$w%(E-Mail Removed)...
> fft1976 wrote:
>> But OOP can be done in C.

>
> I think that's debatable.
>
> You can simulate object-oriented programming in C to an extent, but
> since the language has basically no support, it will inevitably be
> rather "hacky" and complicated.
>


it depends on how one does it...

"some" options get hacky and complicated...
very often though, things just get terribly verbose...

usually the "hacky and complicated" results from people trying in a
misguided attempt for "maximum performance" and thus implementing their
whole damn object system in terms of nested structs and casting, ...

the other alternative is to force an opaque API, which can largely avoid
much of the horror, but does not have the same air of "maximum performance"
about it (typically because accessing a field typically involves a function
call, a switch, and maybe a few pointer-ops...).

the switch can be eliminated if one is willing to sacrifice features, or
require a separate API function for each type of field.


> The gtk+ library for C is a good example of a C library which
> extensively uses OO techniques. However, the resulting code is
> necessarily uglier and less efficient than the equivalent C++ code would
> be. (For example, when using gtk+, every single pointer cast from one
> type to another, even when it's from a derived object type to a base
> object type, is done dynamically at runtime, with runtime checks.)
>


the GTK+ library is a good example of almost pure horror...

then again, in GPL land there are much worse offenders in the land of "pure
horror"...


as for the common horror and hackiness seen when people attempt things like
this:
often, this is the result of people trying to move directly from an OOPL to
C, and just trying to (directly) force their existing practices onto C,
rather than adapting to a more "C-appropriate" approach to problems...

for example, if we take OO in the more abstract sense (AKA, in the more
'philosophical' sense promoted by 'H.S. Lahman' and friends over in
comp.object), then the problem need not turn into an ugly mess (since, hell,
there is no real reason that OOP should look anything like the approaches we
usually see in OOPL's...).


for example, see the Linux kernel, which would seem to be applying some
amount of 'OOP' as well, but has generally refrained from the obtuse
hackiness of GTK and friends, mostly because problems are abstracted and
modularized, and not because of large amounts of hacky "struct-ninjitsu"...


granted, in general the code-quality in most of open-source land is not
exactly to the highest standards...


>> Generic programming on the other hand ...
>>
>> Good luck using macros for that! It's "possible", but my many
>> inquiries into whether there is a macros-based STL analog in the C
>> world turned up nothing.

>
> There are many things doable with templates which are impossible to do
> in C with precompiler macros.


the bigger issue (in your examples) is not the lack of templates, but the
lack of iostream...

but, on the same token, I can note that there are many things one can do is
LISP macros which are impossible to do with C++ templates... (due to, for
example, LISP macros being turing complete and having full access to the
language...).

as well, although the C-preprocessor is limited, it does not rule out the
possibility of custom preprocessors (but, then one can debate that by the
time such a tool includes certain features, such as a full parser, it is no
longer a preprocessor, rather it is a compiler...).



 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-15-2009
BartC wrote:
>> template<typename T>
>> void foo(T value)
>> {
>> std::cout << "The value is: " << T << std::endl;
>> }

>
> You mean << value << here?


Yes. Braino.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-15-2009
ld wrote:
>> You can simulate object-oriented programming in C to an extent, but
>> since the language has basically no support, it will inevitably be
>> rather "hacky" and complicated.

>
> Wrong.


I have yet to see a clean solution.

> Do you _really_ know what is doable with C macros?


I'm pretty sure you cannot resolve the proper format string to use
with printf() to print something you got as a macro parameter
(especially if that something is eg. a struct instantiation). I'm also
pretty sure you cannot eg. resolve whether a parameter is of an integral
or a non-integral type.

> But this not OOP.


So?
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-15-2009
BGB / cr88192 wrote:
> but, on the same token, I can note that there are many things one can do is
> LISP macros which are impossible to do with C++ templates... (due to, for
> example, LISP macros being turing complete and having full access to the
> language...).


Btw, AFAIK it has been proven that the C++ template metalanguage is
turing-complete as well...

You can do surprising things with template metaprogramming (all at
compile time), such as linked lists (with many operations familiar from
lisp, such as getting the first element and the list tail, etc), binary
trees, resolving whether a given integer is prime or not... I once even
wrote an ascii mandelbrot set generator using template metaprogramming,
which the compiler calculated at compile-time (the resulting program
just printed the resulting string and nothing more).
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      07-15-2009
Juha Nieminen <(E-Mail Removed)> writes:

> You can do surprising things with template metaprogramming (all at
> compile time), such as linked lists (with many operations familiar from
> lisp, such as getting the first element and the list tail, etc), binary
> trees, resolving whether a given integer is prime or not...


I think you've put your finger on the problem with template
metaprogramming: people tend to do surprising things with it.
Programming should not be surprising.
--
Ben Pfaff
http://benpfaff.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: New release of the Dynace OO extension to C fft1976 C Programming 202 08-10-2009 04:51 AM
Re: New release of the Dynace OO extension to C Juha Nieminen C++ 58 08-09-2009 02:00 PM
Re: New release of the Dynace OO extension to C Juha Nieminen C Programming 68 08-09-2009 02:00 PM
Dynace OO Extension to C and Windows Development System Blake McBride C++ 0 01-09-2008 03:44 AM
Dynace OO Extension to C and Windows Development System Blake McBride C Programming 0 01-09-2008 03:44 AM



Advertisments