【问题标题】:boost-mpl, fold and placeholders, select class from vectorboost-mpl,折叠和占位符,从向量中选择类
【发布时间】:2013-06-05 12:47:57
【问题描述】:

我正在尝试学习 C++ 模板元编程。给定一个 boost::mpl::vector 类,我想计算该类的索引,其中静态成员变量具有特定值。

我找到了一个似乎可行的解决方案。但是,为了正确编译,我需要一些奇怪的“包装类”,这似乎是不必要的。这是我的代码:

#include <iostream>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/range_c.hpp>

using namespace boost;

template<typename T>
struct get_ind {
    typedef mpl::int_<T::type::value> type;
};

template <typename T>
struct get_x {
typedef mpl::int_<T::x> type;
};

template<typename l>
struct clist {
typedef mpl::range_c<int, 0, mpl::size<l>::type::value > indices;
typedef mpl::fold<
    indices, mpl::size<l>,
    mpl::if_<
        is_same<

// HERE:
    get_x<mpl::at<l, get_ind<mpl::placeholders::_2> > >
// 
//  mpl::int_< mpl::at<l, mpl::placeholders::_2>::type::x > 
//  mpl::int_<mpl::at<l, mpl::placeholders::_2> >::x >  
        , mpl::int_<1>   >  
                     ,
    mpl::placeholders::_2, mpl::placeholders::_1 >
> index;
};


struct A {
static const int x = 1;
};

struct B {
static const int x = 0;
};


int main(int argc, char*argv[]) {

typedef boost::mpl::vector<A, B> classes;
typedef clist<classes> classlist;

std::cout << "result " << classlist::index::type::value<<std::endl;
return 0;
}

编辑:

我现在已经确定它确实可以编译。但是,史蒂文的建议也不起作用。对于该更改,我收到以下错误:

test.cpp: In instantiation of ‘clist<boost::mpl::vector<A, B, mpl_::na, mpl_::na,     
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_    
::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na> >’:
test.cpp:56:   instantiated from here
test.cpp:38: error: ‘x’ is not a member of ‘mpl_::void_’
test.cpp: In function ‘int main(int, char**)’:
test.cpp:56: error: ‘classlist::index’ is not a class or namespace

谁能向我解释我的第一个解决方案有什么问题(已注释掉)以及如何避免对 get_x 和 get_ind 类的需要?

非常感谢

【问题讨论】:

  • 在我的环境中它不能编译。请在其他包含后添加#include &lt;boost/mpl/range_c.hpp&gt;using namespace boost;。添加此内容并尝试您的其他变体后,除了许多其他错误之外,结果是语法错误。在工作解决方案中是 3 &lt; 和 3 &gt;,在不工作解决方案中是 2 &lt; 和 3 &gt;。你必须解决这个问题,我认为你应该在格式化和记录方面做更多的工作。

标签: c++ compiler-errors fold boost-mpl


【解决方案1】:

根据错误消息,您似乎需要类似的东西

mpl::int_< mpl::at<l, mpl::placeholders::_2>::type::x > > 

【讨论】:

    【解决方案2】:

    我们需要向 if_ 传递一个元函数,它可以在折叠展开后进行惰性求值。

    mpl::int_< mpl::at<l, mpl::placeholders::_2>::type::x >
    

    它会立即进行求值,从而产生无法从表达式中找到“x”的错误。

    您可以尝试使用装扮好的测试函数而不是 is_same,例如

    template <typename T, typename V>
    struct has_value
        : mpl::bool_<T::x == V::value>
    {};
    
    template<typename l>
    struct clist {
      typedef mpl::range_c<int, 0, mpl::size<l>::type::value > indices;
      typedef mpl::fold<
        indices, mpl::size<l>,
        mpl::if_<
          has_value<
            mpl::at<l, mpl::placeholders::_2>
            , mpl::int_<1>   >  
          ,
          mpl::placeholders::_2, mpl::placeholders::_1 >
        > index;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多