【问题标题】:In boost MPL, how do I check if an operation worked as intended?在 boost MPL 中,如何检查操作是否按预期工作?
【发布时间】: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 就是一个很好的例子)。

【问题讨论】:

    标签: c++ boost boost-mpl


    【解决方案1】:
    1. equal_tomodels the Numeric Metafunction concept。你会想使用equal
    2. 你会想在比较之前应用元函数,我在断言中添加了::type
    3. 预期的类型实际上并不匹配,因此除非您匹配它们,否则它将失败。

    Live On 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> ));
    

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多