使用boost :: mpl获取向量的大小

时间:2015-10-20 08:37:29

标签: c++ boost

我正在尝试编译此代码&我是通用编程的新手。意图是在boost :: mpl中获取向量的大小。 尝试使用boost :: mpl。我无能为力,为什么这段代码不会混淆。

错误:&#39>输入'在班级' vectorsum>'没有命名类型

#include <boost/mpl/distance.hpp>
#include <boost/static_assert.hpp>
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/greater_equal.hpp>
#include <boost/mpl/push_back.hpp>
#include <boost/mpl/times.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/push_back.hpp>

using namespace boost::mpl;

template <typename Seq>
struct vectorsum_impl
{
    typedef typename boost::mpl::begin<Seq>::type typestart;
    typedef typename boost::mpl::end<Seq>::type  typeend;
    typedef typename boost::mpl::distance<typestart,typeend>::type half_size;
};

template <typename S>
struct vectorsum: vectorsum_impl<S> {};

typedef boost::mpl::vector_c<int, 1, 2, 3, 4> testVec;
typedef vectorsum<testVec>::type testVec2;

main()
{
}

1 个答案:

答案 0 :(得分:1)

为了获得增强MPL序列的大小,我建议使用boost::mpl::size

#include <boost/mpl/size.hpp>
#include <boost/mpl/vector_c.hpp>
#include <iostream>

int main()
{
    typedef boost::mpl::vector_c<int, 1, 2, 3, 4> testVec;
    std::cout << boost::mpl::size<testVec>::value;
    return 0;
}

<强>输出:

4

live example