C
Clint Olsen
I was just thinking about the virtues of C vs. C++ wrt. ADT/generic
programming. The biggest complaint about writing container libraries for
ADTs is that void * offers no type safety. Does it really have to be this
way?
Couldn't you for instance track an object's accesses with void pointers and
ensure they are used consistently across calls?
---------
typedef struct {
size_t count;
/* ... */
void *data;
} array_t;
int array_push(array_t *array, void *data);
void *array_pop(array_t *array);
---------
Based on these two interface functions, we can on an individual object of
type array_t track that the parameter passed to push() has the same type as
the one to which you would assign from the pop() function.
Of course, for functions that take multiple void arguments or use void
arguments in inconsistent ways, we would need to track how those arguments
are used and are eventually assigned/read to/from the members of type
array_t.
This would appear to work in theory for code in which you have all the
source files available. It falls apart however when you only have a header
file as a specification for a pre-compiled library. I suppose since the
parameter identifiers are optional in function prototypes that you might be
able to divine something if the programmer elects to use some sort of
predictable naming for the void arguments.
At any rate, it was just a random thought. My idea is full of holes but it
was at least in the spirit of C
Thanks,
-Clint
programming. The biggest complaint about writing container libraries for
ADTs is that void * offers no type safety. Does it really have to be this
way?
Couldn't you for instance track an object's accesses with void pointers and
ensure they are used consistently across calls?
---------
typedef struct {
size_t count;
/* ... */
void *data;
} array_t;
int array_push(array_t *array, void *data);
void *array_pop(array_t *array);
---------
Based on these two interface functions, we can on an individual object of
type array_t track that the parameter passed to push() has the same type as
the one to which you would assign from the pop() function.
Of course, for functions that take multiple void arguments or use void
arguments in inconsistent ways, we would need to track how those arguments
are used and are eventually assigned/read to/from the members of type
array_t.
This would appear to work in theory for code in which you have all the
source files available. It falls apart however when you only have a header
file as a specification for a pre-compiled library. I suppose since the
parameter identifiers are optional in function prototypes that you might be
able to divine something if the programmer elects to use some sort of
predictable naming for the void arguments.
At any rate, it was just a random thought. My idea is full of holes but it
was at least in the spirit of C
Thanks,
-Clint