在boost MPL中,如何检查操作是否按预期工作
In boost MPL, how do I check if an operation worked as intended?
通常,当我编写代码时,我经常检查我所做的工作是否有效,但要使用某种断言操作:
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
中实现这一点?我目前正试图从两个向量生成一个成对的向量,其中第一个向量表示密钥,第二个向量表示值(即类型):
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
就是一个很好的例子)。
equal_to
为数值元函数概念建模。您可能希望使用equal
- 您将希望在比较之前应用元函数,我在断言中添加了
::type
- 预期的类型实际上并不匹配,因此除非您匹配它们,否则它将失败
查看Coliru直播
#include <boost/mpl/vector.hpp>
#include <boost/mpl/pair.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/equal.hpp>
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, int>,
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>>;
BOOST_MPL_ASSERT(( boost::mpl::equal<PairsSequence::type, ExpectedOutput> ));
相关文章:
- 某些 boost::asio 异步函数是否将处理程序连接到操作,以便处理程序被触发一次?
- 标记为 std::memory_order_seq_cst 的单个原子操作是否会在所有位置触发顺序一致性?
- 如何检测函数是否执行IO操作
- 当其他线程正在编写线程安全时,我是否必须互斥读操作
- 如果 RMW 操作没有任何变化,是否可以针对所有内存顺序对其进行优化
- 是否有一个C++函数可以准确返回平方根反比的内置 CPU 操作 RSQRTSS 的值?
- 负载是否可以在获取操作下方滑动/存储是否可以漂浮在C++释放上方?
- 如何使客户端在将数据写入 C++ 管道之前检查服务器是否完成了从管道的读取操作
- 我是否缺少<实验/文件系统>移动操作?
- 是否可以访问类数据成员并在析构函数中对它们执行操作?
- 是新的(&*p)双倍;一个无操作。因此,uninitialized_default_construct是否也变得无操作?
- blaze-lib 的元素操作是否自动并行?
- 是否有可能具有放入容器的移动操作的类型?
- 是否可以拦截/检测关闭 PC 并首先执行操作的请求?
- 在链接的程序集文件中,我想从 c++ 调用代码访问变量.是否可以在不触发访问冲突的情况下执行此操作?
- 编译器是否会简化按顺序执行多次的操作
- 在C++中,当表达式涉及对象时,将表达式赋值到对象中时,是否有定义的操作顺序?
- 静态断言添加操作是否可用
- 我是否应该避免在循环中的常数之间进行操作
- 我们是否可以使用 SEAL / PySEAL 库对加密数据执行除法操作Microsoft?