gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:03:38 27 4
gpt4 key购买 nike

我想使用编译时 (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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com