qi preprocessor macros¶
Detailed Description¶
Variadic Templates¶
Overview¶
libqi provides macros to emulate C++11 variadic templates. The idea is to transform a variadic definition:
template <typename R, typename... T>
R forward(T... args) {
return call<R, T...>(args...);
}
into multiple normal definitions:
template <typename R>
R forward() {
return call<R>();
}
// ...
template <typename R, typename T1, typename T2, typename T3>
R forward(T1 arg1, T2 arg2, T3 arg3) {
return call<R, T1, T2, T3>(arg1, arg2, arg3);
}
The downside is that it makes compilation time longer and does not support arguments up to infinity.
Usage¶
With the previous example, the macro usage looks like:
#define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
template <typename R comma ATYPEDECL> \
R forward(ADECL) \
{ \
/* note that you can skip "comma ATYPES" since they can \
* be automatically inferred in this case */ \
return call<R comma ATYPES>(AUSE); \
}
QI_GEN(genCall)
#undef genCall
You must define a macro that will be called with 0, 1, ..., n arguments and must generate a definition each time.
nis the number of arguments this call should generate.ATYPESDECLis the list of typenames with their names separated by commas.ATYPESis just the names of the template arguments.ADECLis the declaration of each parameter with a name.AUSEis the names of the parameters.commais a comma when there is at least an argument, nothing otherwise. This is needed for the first call in which there is no template argument and the comma must be skipped.