c++ - MPL for_each 使用带有更多参数的仿函数

标签 c++ boost

我想使用编译时 (MPL) for_each 检查给定的输入变量是否在 MPL 数组中,并再次从 MPL 数组中获取和获取输出变量。我正在尝试使用具有 2 个参数的函数对象,即 MPL 类型和输入。

#include <boost/mpl/list.hpp>
#include <algorithm>
#include <boost/mpl/for_each.hpp>
#include <string>
#include <istream>
#include <ostream>
#include <sstream>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/bind.hpp>


using namespace std;

namespace mpl = boost::mpl;


typedef mpl::range_c<char,1,5> range5;
typedef mpl::list<
mpl::int_<1>
, mpl::int_<5>
, mpl::int_<31>
, mpl::int_<14>
, mpl::int_<51>
>  out_type;


template <class T> struct id {};
struct do_this_wrapper {
    static char stat_c ;
    template<typename U> inline void operator()(int i, U )
    {
        if (i == U::value)
        {
            do_this_wrapper::stat_c = mpl::at_c<out_type,U::value>::type::value;
        }
    }
};


char do_this_wrapper::stat_c ;


int main()
{

    int x =1;
    boost::mpl::for_each<range5>(boost::bind(do_this_wrapper(), x, _1));

    return 0;
};

这些是错误

In file included from /usr/include/boost/bind.hpp:22:0,
                 from ../src/TestProj3.cpp:2627:
/usr/include/boost/bind/bind.hpp: In instantiation of ‘struct boost::_bi::result_traits<boost::_bi::unspecified, do_this_wrapper>’:
/usr/include/boost/bind/bind_template.hpp:15:48:   required from ‘class boost::_bi::bind_t<boost::_bi::unspecified, do_this_wrapper, boost::_bi::list2<boost::_bi::value<int>, boost::arg<1> > >’
../src/TestProj3.cpp:2665:70:   required from here
/usr/include/boost/bind/bind.hpp:69:37: error: no type named ‘result_type’ in ‘struct do_this_wrapper’
     typedef typename F::result_type type;
                                     ^
In file included from ../src/TestProj3.cpp:2617:0:
/usr/include/boost/mpl/for_each.hpp: In instantiation of ‘static void boost::mpl::aux::for_each_impl<false>::execute(Iterator*, LastIterator*, TransformFunc*, F) [with Iterator = boost::mpl::r_iter<mpl_::integral_c<char, '\001'> >; LastIterator = boost::mpl::r_iter<mpl_::integral_c<char, '\005'> >; TransformFunc = boost::mpl::identity<mpl_::na>; F = boost::_bi::bind_t<boost::_bi::unspecified, do_this_wrapper, boost::_bi::list2<boost::_bi::value<int>, boost::arg<1> > >]’:
/usr/include/boost/mpl/for_each.hpp:101:97:   required from ‘void boost::mpl::for_each(F, Sequence*, TransformOp*) [with Sequence = boost::mpl::range_c<char, '\001', '\005'>; TransformOp = boost::mpl::identity<mpl_::na>; F = boost::_bi::bind_t<boost::_bi::unspecified, do_this_wrapper, boost::_bi::list2<boost::_bi::value<int>, boost::arg<1> > >]’
/usr/include/boost/mpl/for_each.hpp:111:38:   required from ‘void boost::mpl::for_each(F, Sequence*) [with Sequence = boost::mpl::range_c<char, '\001', '\005'>; F = boost::_bi::bind_t<boost::_bi::unspecified, do_this_wrapper, boost::_bi::list2<boost::_bi::value<int>, boost::arg<1> > >]’
../src/TestProj3.cpp:2665:71:   required from here
/usr/include/boost/mpl/for_each.hpp:75:25: error: no match for call to ‘(boost::_bi::bind_t<boost::_bi::unspecified, do_this_wrapper, boost::_bi::list2<boost::_bi::value<int>, boost::arg<1> > >) (mpl_::integral_c<char, '\001'>&)’
         aux::unwrap(f, 0)(boost::get(x));

请问是否可以这样使用以及如何使用

最佳答案

您应该为 do_this_wrapper 实现 BOOST_RESULT_OF 协议(protocol):

    typedef void result_type;

查看 Live On Coliru

#include <boost/mpl/list.hpp>
#include <algorithm>
#include <boost/mpl/for_each.hpp>
#include <string>
#include <istream>
#include <ostream>
#include <sstream>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/bind.hpp>


using namespace std;

namespace mpl = boost::mpl;


typedef mpl::range_c<char,1,5> range5;
typedef mpl::list<
mpl::int_<1>
, mpl::int_<5>
, mpl::int_<31>
, mpl::int_<14>
, mpl::int_<51>
>  out_type;


template <class T> struct id {};
struct do_this_wrapper {
    typedef void result_type;
    static char stat_c ;
    template<typename U> inline void operator()(int i, U )
    {
        if (i == U::value)
        {
            do_this_wrapper::stat_c = mpl::at_c<out_type,U::value>::type::value;
        }
    }
};


char do_this_wrapper::stat_c ;


int main()
{

    int x =1;
    boost::mpl::for_each<range5>(boost::bind(do_this_wrapper(), x, _1));

    return 0;
};

关于c++ - MPL for_each 使用带有更多参数的仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24310131/

相关文章:

c++ - 使用 Boost float 比较获取 bool 返回值

c++ - 如何让 MPI_Send 让处理器按顺序发送而不是随机发送?

c++ - wchar_t 在一般编程中有什么用?

c++ - 启动程序

C++ Mutexes-检查是否有另一个线程在等待

c++ - 使用 boost 的 async_write 的异步 tcp 服务器会导致错误的文件描述符

c++ - 尽管包含 header ,但g++链接器无法找到函数

c++ - C++ 中的 4 点 OpenCV 裁剪透视

c++ - 如何使用 std::vector 初始化 boost::random::discrete_distribution?

c++ - 如何仅用宏生成整数序列?