例如,我有一系列函数f1,f2等,具有相同的两个参数类型。我想使用宏
RUN((f1)(f2)(f3), a, b)
使用结果
运行函数序列f1(a, b), f2(a, b), f3(a, b)
我认为增强预处理器可以提供帮助。我试过了
#define RUN_DETAIL(pR, pData, pF) pF(a, b);
#define RUN(pFs, a, b) BOOST_PP_SEQ_FOR_EACH(RUN_DETAIL, BOOST_PP_EMPTY, pFs)
但失败了。怎么做?
找到如下答案
#define RUN_DETAIL(pR, pData, pF) pF pData;
#define RUN(pFs, ...) BOOST_PP_SEQ_FOR_EACH(RUN_DETAIL, (__VA_ARGS__), pFs)
此技术也适用于调用宏序列。
答案 0 :(得分:6)
您不需要在这里使用宏。见 Live On Coliru :
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/algorithm.hpp>
#include <boost/phoenix.hpp>
template <typename... F>
struct sequence_application
{
explicit sequence_application(F... fs) : fs(fs...) { }
template <typename... Args>
void operator()(Args const&... args) const {
namespace phx = boost::phoenix;
using namespace phx::arg_names;
boost::fusion::for_each(fs, phx::bind(arg1, phx::cref(args)...));
}
private:
std::tuple<F...> fs;
};
template <typename... F>
sequence_application<F...> apply_all(F&&... fs) {
return sequence_application<F...>(std::forward<F>(fs)...);
}
让我们来证明一下:
#include <iostream>
#include <string>
void foo(const char* v) { std::cout << __FUNCTION__ << ": " << v << "\n"; }
void bar(std::string v) { std::cout << __FUNCTION__ << ": " << v << "\n"; }
struct poly_functor {
template <typename... T>
void operator()(T&...) const { std::cout << __PRETTY_FUNCTION__ << "\n"; }
};
您当然可以在问题中进行直接调用:
poly_functor pf;
apply_all(&foo, &bar, pf)("fixed invocation is boring");
但是,这确实很无聊。怎么样,我们保留复合函数,并将它传递给另一个算法?
auto combined = apply_all(&foo, &bar, pf);
boost::for_each(
std::vector<const char*> {"hello", "world", "from", "various"},
combined);
现在,尝试使用您的宏方法。宏是不 C ++中的一流语言公民。
最后,让我们展示它适用于可变参数列表:
struct /*anonymous*/ { int x, y; } point;
// the variadic case
apply_all(pf)("bye", 3.14, point);
完整的演示打印:
foo: fixed invocation is boring
bar: fixed invocation is boring
void poly_functor::operator()(T &...) const [T = <char const[27]>]
foo: hello
bar: hello
void poly_functor::operator()(T &...) const [T = <const char *const>]
foo: world
bar: world
void poly_functor::operator()(T &...) const [T = <const char *const>]
foo: from
bar: from
void poly_functor::operator()(T &...) const [T = <const char *const>]
foo: various
bar: various
void poly_functor::operator()(T &...) const [T = <const char *const>]
void poly_functor::operator()(T &...) const [T = <char const[4], const double, const <anonymous struct at test.cpp:54:5>>]