簡體   English   中英

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

[英]Getting size of vector using boost::mpl

我正在嘗試編譯此代碼,我是通用編程的新手。目的是在boost :: mpl中獲取向量的大小。 嘗試提升:: mpl。 我不知道為什么這段代碼不符合要求。

錯誤:類“ 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()
{
}

為了獲得增強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

現場例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM