我正在尝试使用boost.mpl编写一个小型元程序,该程序匹配"命名通道"使用两个频道地图之间的音频格式。
Name也是一个整数(枚举)。 我试图在运行时实现的一个简单示例可能如下所示:
typedef int Name;
std::map<int, int> map_channels(const std::map<int, Name>& m1, const std::map<Name, int>& m2)
{
std::map<int, int> result;
for (std::map<int, Name>::const_iterator it = m1.begin(); it != m1.end(); ++it)
{
result[it->first] = m2.find(it->second)->second; // Ignoring errors for example
}
return result;
}
int main()
{
std::map<int, Name> m1;
m1[0] = 20;
m1[1] = 22;
m1[2] = 21;
std::map<Name, int> m2;
m2[19] = 1;
m2[20] = 2;
m2[21] = 0;
m2[22] = 3;
std::map<int, int> m3 = map_channels(m1, m2);
for (std::map<int, int>::iterator it = m3.begin(); it != m3.end(); ++it)
{
std::cerr << it->first << ":" << it->second << std::endl;
}
return 0;
}
我正在尝试使用boost.mpl编写编译时版本,到目前为止有类似的内容:
typedef map<
pair<int_<0>, int_<20> >
, pair<int_<2>, int_<22> >
, pair<int_<3>, int_<21> >
> m1;
typedef map<
pair<int_<19>, int_<1> >
, pair<int_<20>, int_<2> >
, pair<int_<21>, int_<0> >
, pair<int_<22>, int_<3> >
> m2;
我想要做的是从其他两张地图创建第三张地图,如下所示:
typedef map<
pair<int_<0>, int_<2> >
, pair<int_<2>, int_<3> >
, pair<int_<3>, int_<0> >
> mapping;
我尝试了以下想法:
// TODO : Dont know how to obtain a vector of keys from a mpl map
typedef vector<int_<0>, int_<2>, int_<3> > m1_keys;
//typedef transform<m1, first<_1> >::type m1_keys; // This does not work. I tried a number of ideas and none worked
// Create the composed function that given a key for m1, looks up the value and then uses that value as a lookup to m2
// This is working fine.
typedef at<m2, at<m1, _1> > composed_m1_m2_expr;
typedef lambda<composed_m1_m2_expr>::type composed_m1_m2_lambda;
// TODO : This fold is no good eithre.
typedef iter_fold<m1_keys, map<>, insert<_1, pair<deref<_2>, apply<composed_m1_m2_lambda, _2 > > > >::type mapping;
注意:我打印出以下内容的值:
struct print_iipair
{
template <typename data_tpt>
void operator()(data_tpt p)
{
(void)p;
std::cerr << "k: " << data_tpt::first::value << ", v: " << data_tpt::second::value << std::endl;
//std::cerr << "value: " << typeid(p).name() << std::endl;
}
};
struct print_value
{
template <typename data_tpt>
void operator()(data_tpt p)
{
(void)p;
std::cerr << "value: " << data_tpt::value << std::endl;
//std::cerr << "value: " << typeid(p).name() << std::endl;
}
};
...
for_each<m1_keys,_>(print_value());
for_each<mapping,_>(print_iipair());
我的问题是:
1)我如何创建一个包含boost :: mpl :: map键的boost :: mpl :: vector?
2)我如何实现iter_fold?我认为问题是围绕申请和_2使用,但不知道如何克服这个问题
感谢。
答案 0 :(得分:2)
事实证明,解决方案是使用mpl :: fold而不是mpl :: transform,因为它无法转换mpl :: map,因为它会生成与源序列相同类型的容器并使用mpl :: push_back构造它不适用于地图。
我的两个似乎有用的解决方案是:
1)我如何创建一个包含boost :: mpl :: map键的boost :: mpl :: vector?
// Get a mpl::vector containing just the keys from the map m1
typedef fold<m1, vector<>, push_back<_1, first<_2> > >::type m1_keys;
2)我如何实现iter_fold?
// Create a mapping
typedef at<m2, at<m1, _1> > composed_m1_m2_expr;
typedef lambda<composed_m1_m2_expr>::type composed_m1_m2_lambda;
typedef fold<m1_keys, map<>, insert<_1, pair<_2, composed_m1_m2_lambda::apply<_2> > > >::type mapping;