Re: Is it possible to make void * safer?
- From: Ian Collins <ian-news@xxxxxxxxxxx>
- Date: Fri, 24 Feb 2006 09:36:04 +1300
websnarf@xxxxxxxxx wrote:
Agreed, that's one big hit when simulating templates over using C++ templates. C++ compiles will inline the trivial wrapper functions.
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.
Yes it will complain, based on my earlier macros:
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.
#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.
.
- Follow-Ups:
- Re: Is it possible to make void * safer?
- From: websnarf
- Re: Is it possible to make void * safer?
- References:
- Is it possible to make void * safer?
- From: Clint Olsen
- Re: Is it possible to make void * safer?
- From: websnarf
- Re: Is it possible to make void * safer?
- From: Clint Olsen
- Re: Is it possible to make void * safer?
- From: Ian Collins
- Re: Is it possible to make void * safer?
- From: websnarf
- Re: Is it possible to make void * safer?
- From: Ian Collins
- Re: Is it possible to make void * safer?
- From: websnarf
- Is it possible to make void * safer?
- Prev by Date: Re: Subtracting pointers: Which type to use to store the result?
- Next by Date: Re: Subtracting pointers: Which type to use to store the result?
- Previous by thread: Re: Is it possible to make void * safer?
- Next by thread: Re: Is it possible to make void * safer?
- Index(es):
Relevant Pages
|