Francois Grieu
10/22/2009 6:03:00 AM
Peter Nilsson wrote :
> Francois Grieu wrote:
>> Peter Nilsson wrote:
>>> Here's something that achieves the same basic result, but
>>> in a (marginally) more maintainable way... [snip]
>> Paraphrasing:
>>
>> #define TABLE(macro) >> macro( "the answer is", 6, 7 ) >> macro( "foo", 1, 1 ) >> macro( "bar", 2, 1 ) >> macro( "zoo", 3, 1 ) >> #define TABLEstr( str, num1, num2 ) str,
>
> I pass the delimiter as a second parameter. In C89, the
> grammar does not cater for the last enumerator having a
> comma. [C99 fixed this.]
All C compilers (including for embedded CPUs ) that I use
allow the final comma in constant declarations, even though
they claim only C89/C90 compatibility. And checking that
grammar, I read
3.5.7 Initialization
Syntax
initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }
initializer-list:
initializer
initializer-list , initializer
Further, all but one C compiler that I use also allow the final
comma in an enum (which indeed is not in C89/C90), and the vendor
for said compiler has promised to improve that real soon now.
> why does it have to be _self_ inclusion?
Of course C does not require it, and most of the time
using a separate file is just fine. But there might be a
rule that the interface and implementation of a compilation
unit is split in exactly two files named like "foo.h"
and "foo.c". And the convention that the interface for a
compilation unit is exactly one file named like "foo.h"
is very common.
See Message-ID: <4ade2fde$0$405$426a74cc@news.free.fr>
>> Also, when applied to computing the size of a struct to
>> define the size of a final field bringing the struct to
>> some fixed size, self-inclusion can look sufficiently
>> similar to something familiar that both an external
>> programmer and a development environment like Visual
>> Studio will recognize the bulk of the struct, and be
>> able to pinpoint where each fields is declared. I can't
>> see how to achieve this with the "macro" technique.
>
> I don't really understand the example you're trying to give.
> Manually padding structs is difficult with or without macros.
> The simple option is to use a union...
The same message points that with union, either you need
anonymous union, or break compatibility with former code.
> [snip example of structure padding with macro]
One advantage of doing basically that with self-inclusion
rather than macro is that the location with struct declaration
and list of fields can be identified as such by a wider range
of programmers, and by development environments like Visual
Studio.
Also, I think that your padding size computation
> [...] 128 - sizeof(struct abc) [...]
is not robust enough. In fact I can't find a single way to
do it in a fully conformant way (and won't post my best
approximation without prior deep thinking about it).
Francois Grieu