Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用boost预处理器运行一系列函数?_C++_Boost_Boost Preprocessor - Fatal编程技术网

C++ 如何使用boost预处理器运行一系列函数?

C++ 如何使用boost预处理器运行一系列函数?,c++,boost,boost-preprocessor,C++,Boost,Boost Preprocessor,例如,我有一系列函数f1、f2等等,它们具有相同的两个参数类型。我想使用宏 RUN((f1)(f2)(f3), a, b) 使用结果运行函数序列 f1(a, b), f2(a, b), f3(a, b) 我认为boost预处理器会有所帮助。我试过了 #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)

例如,我有一系列函数f1、f2等等,它们具有相同的两个参数类型。我想使用宏

RUN((f1)(f2)(f3), a, b)
使用结果运行函数序列

f1(a, b), f2(a, b), f3(a, b)
我认为boost预处理器会有所帮助。我试过了

#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)

此技术也适用于调用宏序列。

您不需要在此处使用宏。看到它了吗

#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)...);
}
但是,那确实很无聊。我们保持复合函子,然后把它传给另一个算法,怎么样

auto combined = apply_all(&foo, &bar, pf);

boost::for_each(
        std::vector<const char*> {"hello", "world", "from", "various"},
        combined);
完整的演示打印:

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>>]
foo:fixed调用很无聊
酒吧:固定调用是无聊的
void poly_functor::operator()(T&…)const[T=]
福:你好
酒吧:你好
void poly_functor::operator()(T&…)const[T=]
傅:世界
酒吧:世界
void poly_functor::operator()(T&…)const[T=]
傅:从
酒吧:从
void poly_functor::operator()(T&…)const[T=]
傅:各种各样的
酒吧:各种各样的
void poly_functor::operator()(T&…)const[T=]
void poly_functor::operator()(T&…)const[T=]

您想要实现什么目标?这似乎是可能的模板。“但失败”-它是如何失败的?关于你的编辑:嗯。不,这是一个完全不同的问题,所以如果你想问这个问题,请分别发帖!对于下面的原始问题,您已经有了一个出色的答案。我已回滚您的编辑。@编辑:不要这样做,只需键入它,这是一次编写代码。=默认值的存在是有原因的。@user1899020它们是不同的问题。事实上,完全不同。我知道你可能不会马上意识到这一点,但请相信我们。他在回答问题时花了那么多的精力,却又改变了问题,这是不好的,因为这会让他的答案看起来好像不适用于你所问的问题。但我认为你已经得到了答案?在预处理器足够的时候拉进凤凰号和融合号对我来说似乎有点过分了。@MartinBa我倾向于认为宏是最后的选择。在这种情况下,能否显示正在工作的宏解决方案?我可能会更新这个函数,它实际上也是一个宏。@user1899020哦,太好了:(这让这个问题很容易让人误解)。@user1899020:你没有“编辑”它;你用一个完全不同的函数替换了它。我把它回滚了。
auto combined = apply_all(&foo, &bar, pf);

boost::for_each(
        std::vector<const char*> {"hello", "world", "from", "various"},
        combined);
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>>]