Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.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++ MPL for_each使用具有更多参数的函子_C++_Boost - Fatal编程技术网

C++ MPL for_each使用具有更多参数的函子

C++ MPL for_each使用具有更多参数的函子,c++,boost,C++,Boost,我想使用编译时(MPL)来检查给定的输入变量是否在MPL数组中,并再次从MPL数组中获取输出变量。我尝试使用带有两个参数的函数对象:MPL类型和输入 #include <boost/mpl/list.hpp> #include <algorithm> #include <boost/mpl/for_each.hpp> #include <string> #include <istream> #include <ostream>

我想使用编译时(MPL)来检查给定的输入变量是否在MPL数组中,并再次从MPL数组中获取输出变量。我尝试使用带有两个参数的函数对象: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;
};
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
名称空间mpl=boost::mpl;
typedef mpl::range_c range 5;
typedefmpl::list<
mpl::int_
,mpl::int_
,mpl::int_
,mpl::int_
,mpl::int_
>out_型;
模板结构id{};
结构做这个包装{
静态字符统计;
模板内联void运算符()(int i,U)
{
如果(i==U::value)
{
do_this_wrapper::stat_c=mpl::at_c::type::value;
}
}
};
char do_this_包装器::stat_c;
int main()
{
int x=1;
boost::mpl::for_each(boost::bind(dou this_wrapper(),x,_1));
返回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));
在/usr/include/boost/bind.hpp:22:0中包含的文件中,
从../src/TestProj3.cpp:2627:
/usr/include/boost/bind/bind.hpp:在“struct boost::_bi::result_traits”的实例化中:
/usr/include/boost/bind/bind_template.hpp:15:48:来自“类boost::_bi::bind_t”的必填项
../src/TestProj3.cpp:2665:70:此处为必填项
/usr/include/boost/bind/bind.hpp:69:37:错误:“struct do\u this\u包装器”中没有名为“result\u type”的类型
typedef typename F::result_type type;
^
在../src/TestProj3.cpp:2617:0中包含的文件中:
/usr/include/boost/mpl/for_each.hpp:“static void boost::mpl::aux::for_each_impl::execute(迭代器*,LastIterator*,TransformFunc*,F)[with Iterator=boost::mpl::r_iter;LastIterator=boost::mpl::r_iter;TransformFunc=boost::mpl::identity;F=boost::\u bi::bind_t]”的实例化:
/usr/include/boost/mpl/for_each.hpp:101:97:void boost::mpl::for_each(F,Sequence*,TransformOp*)[with Sequence=boost::mpl::range\u c;TransformOp=boost::mpl::identity;F=boost:\u bi::bind\u t]中的必填项
/usr/include/boost/mpl/for_each.hpp:111:38:从“void boost::mpl::for_each(F,Sequence*)[with Sequence=boost::mpl::range_c;F=boost::_bi::bind_t]中需要
../src/TestProj3.cpp:2665:71:此处为必填项
/usr/include/boost/mpl/for_each.hpp:75:25:错误:调用“(boost::_bi::bind_t)(mpl:::integral_c&)不匹配
辅助::展开(f,0)(boost::get(x));

我想问一下,这是否可以用这种方式使用,以及您应该如何实现
do\u this\u wrapper
协议的BOOST\u RESULT\u:

    typedef void result_type;
查看它

#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;
};
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
名称空间mpl=boost::mpl;
typedef mpl::range_c range 5;
typedefmpl::list<
mpl::int_
,mpl::int_
,mpl::int_
,mpl::int_
,mpl::int_
>out_型;
模板结构id{};
结构做这个包装{
typedef void result_type;
静态字符统计;
模板内联void运算符()(int i,U)
{
如果(i==U::value)
{
do_this_wrapper::stat_c=mpl::at_c::type::value;
}
}
};
char do_this_包装器::stat_c;
int main()
{
int x=1;
boost::mpl::for_each(boost::bind(dou this_wrapper(),x,_1));
返回0;
};

干杯,如果我更改typedef mpl::range\u c range 5;使用typedef mpl::listinp\u类型;另一个变化是boost::mpl::for_each(boost::bind(dou this_wrapper(),x,_1));我有这些错误。。。错误:“struct boost::mpl::l_end”中没有名为“item”的类型。。。错误:没有名为“next”的类型。。。我尝试了与结果相同的修复类型结果相同感谢详细描述或阅读其他内容提前感谢您能发布SSCCEs以便我可以展示它吗?您需要一个嵌套的结构模板
result
如:
template struct result{typedef void type;}可能(但我无法阅读该评论,因此无法更具体。请参阅)NB。一些库允许使用C++11特性来消除对协议结果的需求(我不知道MPL是否支持这一点)。您可以尝试通过
#定义BOOST_RESULT_OF_USE_DECLTYPE
来激活它,然后再包括任何BOOST header。因为我是堆栈溢出中的新手,所以我回答了我的问题(可能有点意外),但我需要快速的答案。请看。