C++ 使用Boost.MPL一次性定义标签和序列
我对Boost.MPL有一个问题,我不知道如何处理它。当前我的代码 看起来像这样:C++ 使用Boost.MPL一次性定义标签和序列,c++,boost,c++03,boost-mpl,C++,Boost,C++03,Boost Mpl,我对Boost.MPL有一个问题,我不知道如何处理它。当前我的代码 看起来像这样: struct Definition { typedef boost::mpl::int_<5> A; typedef boost::mpl::int_<3> B; typedef boost::mpl::int_<6> C; typedef boost::mpl::int_<1> D; // (...) typedef boost::mpl
struct Definition {
typedef boost::mpl::int_<5> A;
typedef boost::mpl::int_<3> B;
typedef boost::mpl::int_<6> C;
typedef boost::mpl::int_<1> D;
// (...)
typedef boost::mpl::vector<
A
,B
,C
,D
// (...)
> Seq;
};
struct Definition {
FIELD(A, 5);
FIELD(B, 3);
FIELD(C, 6);
FIELD(D, 1);
// (...)
GEN_SEQ() // only if really needed
};
然后Definition::A
仍然会引用boost::mpl::int \
,或者
至少允许我以某种方式访问boost::mpl::int,并且
Definition::Seq
将为我提供适当的MPL序列
当然,这只是我的想象。代码可能看起来不一样,我很抱歉
只是在寻找选择 我认为,为了以编程方式完成所有工作,您需要将字段的名称设置为可访问的名称—如果您仅将它们命名为
A
,B
。。。不是真的。您可以尝试使您的序列同时包含以下两种内容:
using namespace boost::mpl; // so I don't have to keep typing it
typedef map<
pair<char_<'A'>, int_<5>>,
pair<char_<'B'>, int_<3>>,
pair<char_<'C'>, int_<6>>,
...
> Seq;
我将less_than
和addNext
的实现留给您。但是这样你就可以把它加起来:value==14你看到了吗?它具有与您所希望的一样的变量抽象:
#include <mpllibs/metamonad/eval_multi_let_c.hpp>
#include <mpllibs/metamonad/pair.hpp>
#include <mpllibs/metamonad/syntax.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/assert.hpp>
#include <mpllibs/metamonad/metafunction.hpp>
#include <mpllibs/metamonad/lazy_metafunction.hpp>
#include <mpllibs/metamonad/returns.hpp>
#include <mpllibs/metamonad/name.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/times.hpp>
#include <boost/mpl/divides.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/minus.hpp>
#include <boost/mpl/equal_to.hpp>
int main()
{
using namespace mpllibs::metamonad::name;
using boost::mpl::equal_to;
using boost::mpl::plus;
using boost::mpl::map;
using boost::mpl::int_;
using mpllibs::metamonad::eval_multi_let_c;
using mpllibs::metamonad::syntax;
using mpllibs::metamonad::pair;
// test_evaluation_of_expression
static_assert(
equal_to<
int_<14>,
eval_multi_let_c<
map<
pair<a, syntax<int_<5>> >,
pair<b, syntax<int_<3>> >,
pair<c, syntax<int_<6>> >
>,
plus<a, b, c> >::type
>::value, "Yay, maths still work"
);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
使用名称空间mpllibs::metamonad::name;
使用boost::mpl::equal_to;
使用boost::mpl::plus;
使用boost::mpl::map;
使用boost::mpl::int;
使用mpllibs::metamonad::eval_multi_let_c;
使用mpllibs::metamonad::syntax;
使用mpllibs::metamonad::pair;
//表达式的测试和评估
静态断言(
等于<
国际,
评估多个项目<
地图<
一对
一对
一对
>,
加>::键入
>:“是的,数学仍然有效”
);
}
#include <mpllibs/metamonad/eval_multi_let_c.hpp>
#include <mpllibs/metamonad/pair.hpp>
#include <mpllibs/metamonad/syntax.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/assert.hpp>
#include <mpllibs/metamonad/metafunction.hpp>
#include <mpllibs/metamonad/lazy_metafunction.hpp>
#include <mpllibs/metamonad/returns.hpp>
#include <mpllibs/metamonad/name.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/times.hpp>
#include <boost/mpl/divides.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/minus.hpp>
#include <boost/mpl/equal_to.hpp>
int main()
{
using namespace mpllibs::metamonad::name;
using boost::mpl::equal_to;
using boost::mpl::plus;
using boost::mpl::map;
using boost::mpl::int_;
using mpllibs::metamonad::eval_multi_let_c;
using mpllibs::metamonad::syntax;
using mpllibs::metamonad::pair;
// test_evaluation_of_expression
static_assert(
equal_to<
int_<14>,
eval_multi_let_c<
map<
pair<a, syntax<int_<5>> >,
pair<b, syntax<int_<3>> >,
pair<c, syntax<int_<6>> >
>,
plus<a, b, c> >::type
>::value, "Yay, maths still work"
);
}