Czy możliwe jest iterację argumentów w zmiennych makrach?
Zastanawiałem się, czy możliwe jest iterację nad argumentami przekazywanymi do zmiennego makra w C99 lub przy użyciu jakichkolwiek rozszerzeń GCC ?
Na przykład, czy możliwe jest napisanie ogólnego makra, które przyjmuje strukturę i jej pola przekazywane jako argumenty i drukuje przesunięcie każdego pola wewnątrz struktury ?
Coś takiego:
struct a { int a; int b; int c; }; /* PRN_STRUCT_OFFSETS will print offset of each of the fields within structure passed as the first argument. */ int main(int argc, char *argv[]) { PRN_STRUCT_OFFSETS(struct a, a, b, c); return 0; }
9 answers
Oto moja praca domowa dnia, opiera się na sztuczkach makro i dzisiaj szczególnie dowiedziałem się o __VA_NARG__
wynaleziony przez Laurenta Deniau . W każdym razie, poniższy przykładowy kod działa do 8 pól dla jasności. Wystarczy rozszerzyć Kod przez duplikację, jeśli potrzebujesz więcej (dzieje się tak dlatego, że preprocesor nie posiada rekurencji, ponieważ odczytuje plik tylko raz).
#include <stdio.h>
#include <stddef.h>
struct a
{
int a;
int b;
int c;
};
struct b
{
int a;
int b;
int c;
int d;
};
#define STRINGIZE(arg) STRINGIZE1(arg)
#define STRINGIZE1(arg) STRINGIZE2(arg)
#define STRINGIZE2(arg) #arg
#define CONCATENATE(arg1, arg2) CONCATENATE1(arg1, arg2)
#define CONCATENATE1(arg1, arg2) CONCATENATE2(arg1, arg2)
#define CONCATENATE2(arg1, arg2) arg1##arg2
/* PRN_STRUCT_OFFSETS will print offset of each of the fields
within structure passed as the first argument.
*/
#define PRN_STRUCT_OFFSETS_1(structure, field, ...) printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));
#define PRN_STRUCT_OFFSETS_2(structure, field, ...)\
printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
PRN_STRUCT_OFFSETS_1(structure, __VA_ARGS__)
#define PRN_STRUCT_OFFSETS_3(structure, field, ...)\
printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
PRN_STRUCT_OFFSETS_2(structure, __VA_ARGS__)
#define PRN_STRUCT_OFFSETS_4(structure, field, ...)\
printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
PRN_STRUCT_OFFSETS_3(structure, __VA_ARGS__)
#define PRN_STRUCT_OFFSETS_5(structure, field, ...)\
printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
PRN_STRUCT_OFFSETS_4(structure, __VA_ARGS__)
#define PRN_STRUCT_OFFSETS_6(structure, field, ...)\
printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
PRN_STRUCT_OFFSETS_5(structure, __VA_ARGS__)
#define PRN_STRUCT_OFFSETS_7(structure, field, ...)\
printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
PRN_STRUCT_OFFSETS_6(structure, __VA_ARGS__)
#define PRN_STRUCT_OFFSETS_8(structure, field, ...)\
printf(STRINGIZE(structure)":"STRINGIZE(field)"-%d\n", offsetof(structure, field));\
PRN_STRUCT_OFFSETS_7(structure, __VA_ARGS__)
#define PRN_STRUCT_OFFSETS_NARG(...) PRN_STRUCT_OFFSETS_NARG_(__VA_ARGS__, PRN_STRUCT_OFFSETS_RSEQ_N())
#define PRN_STRUCT_OFFSETS_NARG_(...) PRN_STRUCT_OFFSETS_ARG_N(__VA_ARGS__)
#define PRN_STRUCT_OFFSETS_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
#define PRN_STRUCT_OFFSETS_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
#define PRN_STRUCT_OFFSETS_(N, structure, field, ...) CONCATENATE(PRN_STRUCT_OFFSETS_, N)(structure, field, __VA_ARGS__)
#define PRN_STRUCT_OFFSETS(structure, field, ...) PRN_STRUCT_OFFSETS_(PRN_STRUCT_OFFSETS_NARG(field, __VA_ARGS__), structure, field, __VA_ARGS__)
int main(int argc, char *argv[])
{
PRN_STRUCT_OFFSETS(struct a, a, b, c);
printf("\n");
PRN_STRUCT_OFFSETS(struct b, a, b, c, d);
return 0;
}
Który drukuje:
struct a:a-0
struct a:b-4
struct a:c-8
struct b:a-0
struct b:b-4
struct b:c-8
struct b:d-12
EDIT: Oto nieco inna wersja, która stara się być bardziej ogólny. makro FOR_EACH(what, ...)
stosuje what
do każdego innego argumentu na liście argumentów zmiennych.
Więc wystarczy zdefiniować makro, które pobiera pojedynczy argument w ten sposób:
#define DO_STUFF(x) foo(x)
Który będzie stosowany do każdego argumentu na liście. Tak więc, dla Twojego typowego przykładu musisz trochę zhakować, ale nadal pozostaje zwięzły: {]}
#define PRN_STRUCT_OFFSETS_(structure, field) printf(STRINGIZE(structure)":"STRINGIZE(field)" - offset = %d\n", offsetof(structure, field));
#define PRN_STRUCT_OFFSETS(field) PRN_STRUCT_OFFSETS_(struct a, field)
I stosujesz to tak:
FOR_EACH(PRN_STRUCT_OFFSETS, a, b, c);
Na koniec kompletny przykładowy program:
#include <stdio.h>
#include <stddef.h>
struct a
{
int a;
int b;
int c;
};
#define STRINGIZE(arg) STRINGIZE1(arg)
#define STRINGIZE1(arg) STRINGIZE2(arg)
#define STRINGIZE2(arg) #arg
#define CONCATENATE(arg1, arg2) CONCATENATE1(arg1, arg2)
#define CONCATENATE1(arg1, arg2) CONCATENATE2(arg1, arg2)
#define CONCATENATE2(arg1, arg2) arg1##arg2
#define FOR_EACH_1(what, x, ...) what(x)
#define FOR_EACH_2(what, x, ...)\
what(x);\
FOR_EACH_1(what, __VA_ARGS__);
#define FOR_EACH_3(what, x, ...)\
what(x);\
FOR_EACH_2(what, __VA_ARGS__);
#define FOR_EACH_4(what, x, ...)\
what(x);\
FOR_EACH_3(what, __VA_ARGS__);
#define FOR_EACH_5(what, x, ...)\
what(x);\
FOR_EACH_4(what, __VA_ARGS__);
#define FOR_EACH_6(what, x, ...)\
what(x);\
FOR_EACH_5(what, __VA_ARGS__);
#define FOR_EACH_7(what, x, ...)\
what(x);\
FOR_EACH_6(what, __VA_ARGS__);
#define FOR_EACH_8(what, x, ...)\
what(x);\
FOR_EACH_7(what, __VA_ARGS__);
#define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
#define FOR_EACH_NARG_(...) FOR_EACH_ARG_N(__VA_ARGS__)
#define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
#define FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
#define FOR_EACH_(N, what, x, ...) CONCATENATE(FOR_EACH_, N)(what, x, __VA_ARGS__)
#define FOR_EACH(what, x, ...) FOR_EACH_(FOR_EACH_NARG(x, __VA_ARGS__), what, x, __VA_ARGS__)
#define PRN_STRUCT_OFFSETS_(structure, field) printf(STRINGIZE(structure)":"STRINGIZE(field)" - offset = %d\n", offsetof(structure, field));
#define PRN_STRUCT_OFFSETS(field) PRN_STRUCT_OFFSETS_(struct a, field)
int main(int argc, char *argv[])
{
FOR_EACH(PRN_STRUCT_OFFSETS, a, b, c);
printf("\n");
return 0;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-02 17:33:53
Ryzykując zdobycie odznaki archeologa, myślę, że jest niewielka poprawa powyższej odpowiedzi Gregory ' ego przy użyciu techniki z przeciążenia makra na liczbę argumentów
Z foo.h:
// Make a FOREACH macro
#define FE_1(WHAT, X) WHAT(X)
#define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__)
#define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__)
#define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__)
#define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__)
//... repeat as needed
#define GET_MACRO(_1,_2,_3,_4,_5,NAME,...) NAME
#define FOR_EACH(action,...) \
GET_MACRO(__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1)(action,__VA_ARGS__)
// Example
// Some actions
#define QUALIFIER(X) X::
#define OPEN_NS(X) namespace X {
#define CLOSE_NS(X) }
// Helper function
#define QUALIFIED(NAME,...) FOR_EACH(QUALIFIER,__VA_ARGS__)NAME
// Emit some code
QUALIFIED(MyFoo,Outer,Next,Inner) foo();
FOR_EACH(OPEN_NS,Outer,Next,Inner)
class Foo;
FOR_EACH(CLOSE_NS,Outer,Next,Inner)
Cpp foo.H generuje:
Outer::Next::Inner::MyFoo foo();
namespace Outer {namespace Next {namespace Inner {
class Foo;
}}}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:54:06
Jeśli twoja struktura jest opisana za pomocą x-makra, to możliwe jest napisanie funkcji lub makra w celu iteracji wszystkich pól struktury i wydrukowania ich przesunięcia.
#include <stddef.h> // offsetof macro
//--- first describe the structure, the fields, their types
#define X_FIELDS \
X(int, field1) \
X(int, field2) \
X(char, field3) \
X(char *, field4)
//--- define the structure, the X macro will be expanded once per field
typedef struct {
#define X(type, name) type name;
X_FIELDS
#undef X
} mystruct;
//--- "iterate" over all fields of the structure and print out their offset
void print_offset(mystruct *aStruct)
{
#define X(type, name) printf("offset of %s is %d\n", #name, offsetof(mystruct, name));
X_FIELDS
#undef X
}
//--- demonstrate
int main(int ac, char**av)
{
mystruct a = { 0, 1, 'a', "hello"};
print_offset(&a);
return 0;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-10 16:11:09
Rozwiązanie Grzegorza Pakosza zadziałało świetnie. Ale miałem z tym dwa drobne problemy:
-
Kompilując z opcją pedantyczną otrzymałem Ostrzeżenie: "ISO99 wymaga użycia argumentów rest". Jest to spowodowane argumentami variad w pierwszym makrze FOR_EACH_1. Usunięcie tych i zmiana wywołania na FOR_EACH_1 w FOR_EACH_2 usunęło to Ostrzeżenie.
#define FOR_EACH_1(what, x) #define FOR_EACH_2(what, x, ...)\ what(x); \ FOR_EACH_1(what);
-
Ponieważ używałem go w bardzo ogólny sposób, czasami musiałem wywołać makro repeat z tylko 1 argumentem. (Wiem, że nie ma sensu powtarzać elementu 1 razy ;)). Na szczęście rozwiązanie tego problemu było dość proste. Po prostu usuwam parametr x z makra FOR_EACH.
#define FOR_EACH(what, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__)
Tutaj pełna lista z dwoma zmianami:
#define CONCATENATE(arg1, arg2) CONCATENATE1(arg1, arg2)
#define CONCATENATE1(arg1, arg2) CONCATENATE2(arg1, arg2)
#define CONCATENATE2(arg1, arg2) arg1##arg2
#define FOR_EACH_1(what, x) \
what(x)
#define FOR_EACH_2(what, x, ...) \
what(x); \
FOR_EACH_1(what, __VA_ARGS__);
#define FOR_EACH_3(what, x, ...) \
what(x); \
FOR_EACH_2(what, __VA_ARGS__);
#define FOR_EACH_4(what, x, ...) \
what(x); \
FOR_EACH_3(what, __VA_ARGS__);
#define FOR_EACH_5(what, x, ...) \
what(x); \
FOR_EACH_4(what, __VA_ARGS__);
#define FOR_EACH_6(what, x, ...) \
what(x); \
FOR_EACH_5(what, __VA_ARGS__);
#define FOR_EACH_7(what, x, ...) \
what(x); \
FOR_EACH_6(what, __VA_ARGS__);
#define FOR_EACH_8(what, x, ...) \
what(x); \
FOR_EACH_7(what, __VA_ARGS__);
#define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
#define FOR_EACH_NARG_(...) FOR_EACH_ARG_N(__VA_ARGS__)
#define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
#define FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
#define FOR_EACH_(N, what, ...) CONCATENATE(FOR_EACH_, N)(what, __VA_ARGS__)
#define FOR_EACH(what, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-17 00:14:13
Może użyć varargs jako inicjalizacji tablicy i iteracji przez countof (array)? tj. sizeof(array)/sizeof (array[0]). Tablica może być anonimową tablicą C99.
Nie mogę wymyślić innego sposobu na iterację nad var-args makra, ponieważ Nie wiem, jak zrobić cokolwiek z tekstem każdego elementu var-arg. Część var-arg równie dobrze może być pojedynczym argumentem, który zawiera przecinki, ponieważ wszystko, co możesz zrobić z CPP, AFAIK.
Ale oto mój pomysł na iterację nad var-args:
#define countof(a) ( sizeof(a)/sizeof((a)[0]) )
#define MACRO(fd, format, ...) do { int ar_[] = { __VA_ARGS__ }; \
for(int i=0; i<countof(ar_) ; ++i){ \
fprintf(fd, format, ar_[i]); \
} } while(0)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-01 00:58:16
To moje rozwiązanie
enjoy
#include <stdlib.h>
#include <stdio.h>
#define ITERATE_OVER_VARADICT_MACROS( str, ...)\
do{\
int i, _arr_[] = {__VA_ARGS__};\
fprintf(stderr,"msg =%s\n", str); \
for(i=0; i<sizeof(_arr_)/sizeof(int) ; i++){ \
fprintf(stderr,"_arr_[%d]= %d\n", i, _arr_[i] ); \
}\
}while(0)
int main(int argc, char* argv[])
{
ITERATE_OVER_VARADICT_MACROS("Test of iterate over arguments in variadic macros", 10,12, 34);
return 0;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-22 09:31:23
To jest najlepsze, co mogę wymyślić, ze standardem C:
#include <stddef.h>
#include <stdio.h>
// prints a single offset
#define PRN_STRUCT_OFFSET(x, a) printf("&" #x "." #a " = %d\n", offsetof(x, a));
// prints a struct with one member
#define PRN_STRUCT_OFFSETS_1(x, a) PRN_STRUCT_OFFSET(x, a)
// prints a struct with two members
#define PRN_STRUCT_OFFSETS_2(x, a, b) \
PRN_STRUCT_OFFSET(x, a) \
PRN_STRUCT_OFFSET(x, b)
// and so on until some N.
// Boost.Preprocessor might help here, I'm not sure
struct some_struct
{
int a;
void* c;
};
int main(void)
{
PRN_STRUCT_OFFSETS_2(struct some_struct, a, c);
return 0;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-09 07:53:01
Dodaję to jako kolejną odpowiedź. Oto próba zrobienia tego z C++0x, skompilowany z g++ 4.5.0
#include <iostream>
using namespace std;
template<typename L>
inline void for_each(L l)
{
}
template<typename L, typename P, typename... Q>
inline void for_each(L l, P arg, Q... args)
{
l(arg);
for_each(l, args...);
}
int main()
{
for_each([] (int x) { cout << x; }, 1, 2, 3);
return 0;
}
Program drukuje
123
Jednak przy takim podejściu wszystkie parametry przekazywane do wyrażenia lambda muszą mieć ten sam typ, int
w powyższym przykładzie. Jednak lambda pozwalają na przechwytywanie zmiennych takich jak:
int main()
{
int offset = 10;
for_each([offset] (int x) { cout << offset + x << endl; }, 1, 2, 3);
return 0;
}
Który drukuje:
11
12
13
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-10 11:10:41
Jeśli jesteś celem Objective-C
... Sprawdź niesamowite KSVarArgs na Github
KSVarArgs jest zestawem makr zaprojektowanych w celu ułatwienia obsługi argumentów zmiennych w Objective-C. wszystkie makra zakładają, że lista varargs zawiera tylko obiekty objective-c lub struktury podobne do obiektów (przypisywane do ID typu). Makro bazowe ksva_iterate_list() iteruje nad argumentami zmiennej, wywołując blok dla każdego argumentu, dopóki nie napotka kończącego się zera. Pozostałe makra są dla wygody podczas konwersji do wspólnych kolekcji.
/*! @param firstNote NSString that is the only known arg
*/
- (void) observeWithBlocks:(NSString*)firstNote,...{
/*! ksva_list_to_nsarray puts varargs into
new array, `namesAndBlocks`
*/
ksva_list_to_nsarray(firstNote, namesAndBlocks);
/// Split the array into Names and Blocks
NSArray *names = [namesAndBlocks subArrayWithMembersOfKind:NSString.class],
*justBlocks = [namesAndBlocks arrayByRemovingObjectsFromArray:names];
[names eachWithIndex:^(id obj, NSInteger idx) {
[self observeName:obj usingBlock:^(NSNotification *n) {
((void(^)())justBlocks[idx])(n);
}];
}];
}
Przykładowe użycie:
[NSNotificationCenter.defaultCenter observeWithBlocks:
NSViewFrameDidChangeNotification, /// first, named arg
^(NSNotification *m){ [self respondToFrameChange]; }, // vararg
NSTextViewDidChangeSelectionNotification, // vararg
^(NSNotification *z){ [z.infoDict[@"textView"] save]; }, // vararg
nil // must nil-terminate
];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-09 03:05:40