MPL for_each使用带有更多形参的函子
MPL for_each to use functor with more parameters
我想使用编译时间(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, ' 01'> >; LastIterator = boost::mpl::r_iter<mpl_::integral_c<char, ' 05'> >; 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, ' 01', ' 05'>; 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, ' 01', ' 05'>; 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, ' 01'>&)’
aux::unwrap(f, 0)(boost::get(x));
您应该为do_this_wrapper
实现BOOST_RESULT_OF协议:
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;
};
相关文章:
- 使用mem_fun_ref if成员函数需要多个形参
- 通过类的模板形参特化成员模板结构
- 非类型引用形参/实参
- 哪个模板形参在boost::shared_ptr构造函数中使用一个原始指针
- 如何确保迭代器模板形参与模板类的模板形参具有相同的数据类型
- 如何在编译时通过模板形参默认值的名称/指针获取函数的类型
- c++:候选模板被忽略:模板形参显式指定的参数无效
- c++中作为形参的指针
- 哪种方法更适合为函数提供编译时间常数?函数实参与模板形参
- 包含void*结构的函数的Const正确性和形参
- 传递boost::函数,该函数接受一个模板实参作为默认为NULL的形参
- 将右值引用形参强制转换为右值引用
- 给引用形参赋值使对象无效
- 当实参是初始化列表而形参是引用时,重载解析
- const整型模板形参的条件
- 使用作为模板形参提供的基类成员,不带限定符
- 模板类的不同返回类型取决于类的形参
- 模板形参有二义性:无法推断模板实参
- 是否有必要在定义中使用模板形参来引用同一个类?
- c++ std::find()和模板形参