Post by Jeremy[..]
I wanted to use it to count the number of arguments represented by
__VA_ARGS__ in a macro, knowing they were a specific type. Here's a
more complete example of what I'm doing now..
template < size_t NumberOfElements >
struct VariableSizedRecord {
int data[NumberOfElements];
};
#define DeclareVariableSizedRecord( Name, ... ) \
int __rgtemp ##Name[] = {__VA_ARGS__}; \
VariableSizedRecord< sizeof(__rgtemp ##Name) / sizeof(int)> Name =
{{__VA_ARGS__}}
DeclareVariableSizedRecord(RecordA, 0, 1, 2, 3);
DeclareVariableSizedRecord(RecordB, 4, 5, 6);
DeclareVariableSizedRecord(RecordC, 7, 8, 9, 10, 11, 12);
Ah, OK, it makes sense. Easier to maintain if you don't have to count
those elements, especially if there is quite a few of them, right?
On the other hand, how often do you really need to change the
initialiser list, honestly? Yes, otherwise you have to type the size of
the array yourself (ugh!)...
VariableSizedRecord<4> RecordA = { 0,1,2,3 };
VariableSizedRecord<3> RecordA = { 4,5,6 };
Do you really consider that "Declare..." macro so much better?
Post by JeremyI noticed that GCC supports the "compound literal" syntax which
#define DeclareVariableSizedRecord( Name, ... ) \
VariableSizedRecord< sizeof( ((int[]){__VA_ARGS__}) ) / sizeof(int) >
Name = {{__VA_ARGS__}}
It works with what I'm doing now, but I was hoping I could avoid the
_rgtemp##Name declaration and save some space. The types I'm declaring
in the macro are more complex than this example, but that's the idea.
The support for those things is patchy. You could try using VS 2010
(now in beta), they probably have array literals.
As to saving space, you need to verify whether those arrays are ever
actually created, or perhaps they are optimized away by the linker since
there is no reference to them anywhere in the code... As a potential
improvement, try declaring them 'static' as well. As in
#define DeclareVariableSizedRecord( Name, ... ) \
static int __rgtemp ##Name[] = {__VA_ARGS__}; \
//^^^^^^
VariableSizedRecord< sizeof(__rgtemp ##Name) \
/ sizeof(int)> Name = {{__VA_ARGS__}}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask