Re: Is it possible to make void * safer?



websnarf@xxxxxxxxx wrote:

I'm not sure what you're gaining from this function pointer abstraction
since you are requiring, specifically "push##type" to exist in file
scope.

The ability to create an instance of the object, as illustrated by the
example push in my original post.

I was specifically demonstrating how to mimic template in C with macros.


Well, you are also using function pointers. This is a performance
concession, especially if you allow it to have an external interface --
then the compiler can't be "smart", and you eat the indirect function
call cost.

Agreed, that's one big hit when simulating templates over using C++ templates. C++ compiles will inline the trivial wrapper functions.

I think my way works best in practice, *except* for the fact that when
you build many of them for various types, the lack of type safety so
easily leads to utter chaos. Unless you run it through a C++ compiler,
but then its just ironic.

That's why I added the function pointers, they are type safe.


Uhhm ... not on the C compilers I use. In fact, I encourage a related
function pointer abuse in Bstrlib (though, I don't require it -- you
can be to the letter if you want; and they are cases where no real
system is actually going to go wrong) for some cases, and it doesn't
come up as an issue.

Yes it will complain, based on my earlier macros:

#define X( type ) \
typedef struct xX##type { \
void (*push)( array_t*, type ); \
} X##type; \
\
void push##type##( array_t* array, type data ) { \
array_push( array, data); }

#define GET_X( type, name ) \
X##type name; \
name.push = push##type;

typedef int* intp;

X(intp)

int main()
{
GET_X( intp, x );

array_t array;
int n = 0;
float f = 0.0;
x.push( &array, &n );
x.push( &array, &f );
}

Will generate warnings for x.push( &array, &f );

--
Ian Collins.
.



Relevant Pages

  • Re: Is it possible to make void * safer?
    ... unless we require the use of a C++ compiler. ... simply use the C++ language capabilities to do this right via templates? ... array_push(array, (void*)data);} ...
    (comp.lang.c)
  • Re: g++pentium 2.9 compiler problem
    ... 2.2.1/vxWorks 5.5.1 g++pentium 2.9 compiler. ... This Compiler is not initialising properly any of the member array of ... function pointers either 1-dimensional or 2-dimensional. ... You defined an array of pointers to the poiters ...
    (comp.arch.embedded)
  • Re: g++pentium 2.9 compiler problem
    ... 2.2.1/vxWorks 5.5.1 g++pentium 2.9 compiler. ... This Compiler is not initialising properly any of the member array of ... function pointers either 1-dimensional or 2-dimensional. ... You defined an array of pointers to the poiters ...
    (comp.arch.embedded)
  • Re: Hundreds of cases in a switch, optimization?
    ... >> switch statement and make it faster, then your compiler would use it. ... >> setting up an array of function pointers, and so on and so on and the ... there is no reason IMHO to use function pointers here. ...
    (comp.lang.c)
  • Re: Function Pointers and NULL/0
    ... > array of function pointers: ... > PrioFunPreferGroundGoals, ... > Now it is being ported much more widely, and one compiler (that I do ...
    (comp.lang.c)