Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Markus Wichmann <nullplan@gmx.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: template metaprogramming in C? |
| Date | 2012-03-26 11:46 +0200 |
| Organization | 1&1 Internet AG |
| Message-ID | <pgq549-ss7.ln1@voyager.wichi.de.vu> (permalink) |
| References | <jkkcqp$b94$1@speranza.aioe.org> <15270245.366.1332599106789.JavaMail.geo-discussion-forums@vbgx21> |
On 24.03.2012 15:25, Malcolm McLean wrote:
> בתאריך יום שבת, 24 במרס 2012 11:57:11 UTC, מאת Rui Maciel:
>> What are your thoughts on adding a standard form of proper template
>> metaprogramming to the C programming language?
>>
>>
> Standards mean that every plug fits every socket, but only it it's
> compatible. So a British 3 pin plug with square pins will fit any
> British mains socket. But it won't fit an American socket, and
> rightly, because American mains voltage is different and you might
> damage equipment.
>
> Now in programming, think of the data as the plug and the function
> interface as the socket. We want any data held be caller to be passed
> into callee, but only if it's compatible. So say someone writes
>
> double mean(double *x, int N);
>
> That's not very good if our data is floats, or integers, or if it's
> the 'salary' field of an array of struct employees. We've got to
> write special adapter fucntions, just to get the data into a suitable
> format to pass to mean.
>
Then we define the C interface as used for qsort: We need to know:
- How big one array element is
- How big the entire array is
- How to add two array elements
- How to divide an array element by an integer
- Where to store the result
That makes:
void mean(void* res, void* base, size_t size, size_t nmemb, void
(*add)(void*, const void*), void(*div)(*void, size_t));
With the semantics:
res - Where to store the result
base - The start of the array
size - The size of a single array member
nmemb - The number of members
add - Adds two array members and stores the result in the first argument
div - Divides an array member by an integer and stores the result in the
first argument.
With the implementation:
void* mean(void *res, void* base, size_t size, size_t nmemb, void
(*add)(void*, void*), void(*div)(*void, size_t))
{
char* our_base = base;
if (nmemb) memcpy(res, base, size);
for (size_t i = 1; i < nmemb, i++) {
add(res, our_base[i * size]);
}
if (nmemb) div(res, nmemb);
}
So a call to this function for a mean over doubles would be
void double_add(void* a, const void* b)
{
*(double*)a += *(const double*)b;
}
void double_div(void* a, size_t n)
{
*(double*)a /= n;
}
double double_mean(double* b, size_t n) {
double res;
mean(&res, b, sizeof *b, n, double_add, double_div);
return res;
}
Now, _that_'s a simple interface...
Incidentally, this interface is also generic enough to support other
means, like the geometric mean (if you set *add to be code to multiply
and *div to be code to draw the n-th root).
> But there are huge problems with a template approach. If we call
> mean() on an array of floats, we would expect a float back. But the
> mean of an array of integers is not necessarily an integer. The mean
> of an array of long doubles or some custome huge integer type might
> not be representable in double.
So we could always return long double... but that would require a mean
that could always be expressed in a long double
> Then if you've got a really low
> precision float type, really you need to sort the input first before
> summing it.
So that the really small values can even make a dent in the mean? But if
you mix values in the array which make addition idempotent (meaning if
you have two values a and b, then a + b could still equal the greater
value), the small values won't change the outcome anyway, will they?
They might as well be zero. The only possible thing that can happen is
if you have several of those small values so that when added
together they produce a carry over into bits that will still be in the
result. However, you would need a really big load of those values for
the carry to make it to some significant bits. I always consider a few
of the least significant bits of a floating point value fickle.
> This is likely to be overkill for normal use, however.
> There are all sorts of problems, even with this extremely simple
> function. The problem with templates is that most template systems
> don't really address these issues. They mean that the function can be
> passed a double *, int *, or float *, but that's a small win.
>
Right you are. For anything else, you would need to write your own mean
function.
Ciao,
Markus
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-24 11:57 +0000
Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-24 07:25 -0700
Re: template metaprogramming in C? Markus Wichmann <nullplan@gmx.net> - 2012-03-26 11:46 +0200
Re: template metaprogramming in C? Philipp Klaus Krause <pkk@spth.de> - 2012-03-27 21:09 +0200
Re: template metaprogramming in C? Markus Wichmann <nullplan@gmx.net> - 2012-03-28 09:25 +0200
Re: template metaprogramming in C? James Kuyper <jameskuyper@verizon.net> - 2012-03-28 06:59 -0400
Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-28 12:13 -0700
Re: template metaprogramming in C? James Dow Allen <jdallen2000@yahoo.com> - 2012-04-01 02:47 -0700
Re: template metaprogramming in C? Jens Gustedt <jens.gustedt@loria.fr> - 2012-03-24 18:01 +0100
Re: template metaprogramming in C? Nobody <nobody@nowhere.com> - 2012-03-24 22:57 +0000
Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 18:29 -0700
Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-25 15:03 +1300
Re: template metaprogramming in C? nick_keighley_nospam@hotmail.com - 2012-03-25 05:32 -0700
Re: template metaprogramming in C? BGB <cr88192@hotmail.com> - 2012-03-24 17:02 -0700
Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-25 01:21 +0100
Re: template metaprogramming in C? Philipp Klaus Krause <pkk@spth.de> - 2012-03-27 21:17 +0200
Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-28 11:10 +0200
Re: template metaprogramming in C? Nils M Holm <news2009@t3x.org> - 2012-03-28 10:05 +0000
Re: template metaprogramming in C? Willem <willem@toad.stack.nl> - 2012-03-28 13:36 +0000
Re: template metaprogramming in C? Nils M Holm <news2009@t3x.org> - 2012-03-28 14:40 +0000
Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-28 18:32 +0000
Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-28 22:27 +0200
Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-28 21:02 +0000
Re: template metaprogramming in C? Nobody <nobody@nowhere.com> - 2012-03-29 16:30 +0100
Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-29 23:39 +0200
Re: template metaprogramming in C? "BartC" <bc@freeuk.com> - 2012-03-29 23:36 +0100
Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-30 00:55 +0000
Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-30 00:32 +0000
Re: template metaprogramming in C? "BartC" <bc@freeuk.com> - 2012-03-30 10:52 +0100
Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-04-01 09:36 +0200
Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-30 16:32 +1300
Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-31 01:52 -0700
Re: template metaprogramming in C? ImpalerCore <jadill33@gmail.com> - 2012-03-28 08:48 -0700
Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 12:18 +0200
Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:08 +0200
Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:36 +0200
Re: template metaprogramming in C? pete <pfiland@mindspring.com> - 2012-04-06 18:43 -0400
Re: template metaprogramming in C? pete <pfiland@mindspring.com> - 2012-04-06 19:20 -0400
Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:08 +0200
Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 19:01 -0700
Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 19:10 -0700
Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-25 15:25 +1300
Re: template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-25 15:13 +0100
Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-26 07:10 +1300
Re: template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-25 20:31 +0100
Re: template metaprogramming in C? nick_keighley_nospam@hotmail.com - 2012-03-26 00:54 -0700
csiph-web