更新时间:2023-09-03 09:30:58
如果您不愿意指定包装函数的返回类型和参数,Boost.Preprocessor将为您服务:
If you can be bothered to specify the return type and parameters of the wrapped function, Boost.Preprocessor has you covered:
#include <boost/preprocessor/tuple/to_seq.hpp>
#include <boost/preprocessor/seq/for_each_i.hpp>
#include <boost/preprocessor/comma_if.hpp>
#define WRAP_declare_param(r, data, i, paramType) \
BOOST_PP_COMMA_IF(i) paramType _ ## i
#define WRAP_forward_param(r, data, i, paramType) \
BOOST_PP_COMMA_IF(i) _ ## i
#define WRAP_seq(prefix, retType, function, argSeq) \
inline retType prefix ## function ( \
BOOST_PP_SEQ_FOR_EACH_I(WRAP_declare_param, ~, argSeq) \
) { \
return function( \
BOOST_PP_SEQ_FOR_EACH_I(WRAP_forward_param, ~, argSeq) \
); \
}
#define WRAP(prefix, retType, function, ...) \
WRAP_seq(prefix, retType, function, BOOST_PP_TUPLE_TO_SEQ((__VA_ARGS__)))
可以让您编写以下内容:
Which lets you write the following:
// Declared somewhere...
int foo(float, double);
WRAP(p_, int, foo, float, double)
// ^^ Prefix
// ^^^ Return type
// ^^^ Function
// ^^^^^^^^^^^^^ Parameter types
其中扩展到:
inline int p_foo ( float _0 , double _1 ) { return foo( _0 , _1 ); }