【发布时间】:2014-09-30 06:09:43
【问题描述】:
通常当我编写代码时,我会经常检查我所做的工作是否有效,但会使用某种断言操作:
std::vector<int> a(1, 1);
std::vector<int> b = {1};
assert(a == b); // this either works, or breaks in a helpful manner
如何在boost mpl 中实现这一点?我目前正在尝试从 2 个向量生成对向量,其中第一个向量表示键,第二个表示值(即 types):
using Keys = boost::mpl::vector<double, bool, int, char, bool*>;
using Types = boost::mpl::vector<char, char, int, int, int>;
using ExpectedOutput =
boost::mpl::vector<
boost::mpl::pair<double, char>,
boost::mpl::pair<bool, char>,
boost::mpl::pair<int, char>,
boost::mpl::pair<char, int>,
boost::mpl::pair<bool*, int>>;
// now I perform some operation which I _think_ might work
using PairsSequence =
typename boost::mpl::transform<
Keys,
Types,
boost::mpl::pair<boost::mpl::_1, boost::mpl::_2>>;
// Now I attempt to check that it worked
BOOST_MPL_ASSERT(( boost::mpl::equal_to<PairsSequence, ExpectedPairsSequence> ));
但这不起作用,大概是因为boost::mpl::transform的返回类型是一些模板表达式。如何强制将此输出转换为可以与 预期 值进行比较的类型?
其他人如何调试他们的 MPL 代码? Boost 似乎没有提供任何检查操作输出的工具(或者至少我不知道如何使用它们,BOOST_MPL_ASSERT 就是一个很好的例子)。
【问题讨论】: