使用boost::mpl获取向量的大小
Getting size of vector using boost::mpl
我正在尝试编译这段代码&我对泛型编程很陌生。目的是在boost::mpl中获得向量的大小。尝试我的手在boost::mpl。我不知道为什么这段代码不能编译。
错误:类'vectorsum>中的'type'没有命名类型
#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
生活例子
相关文章:
- 获取向量C++中第一个值和最后一个值的和
- 如何从用户那里获取输入并将其存储在向量中?
- 很好的语法来获取对向量/数组数据的大小引用?
- C++获取传递给函数的向量的名称
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 如何从C++的对的排序向量中获取有关给定值的相应对
- push_back通过自行创建的对象获取最后一个元素的向量
- 从自定义类获取对象向量中的 max 元素
- 当映射包含字符串向量作为值时,从值中获取键的有效方法
- 沿向量获取点
- 如何在指定数据之间将数据从一个向量获取到另一个向量
- 从向量获取值
- 通过函数参数从向量获取类指针
- C++:从向量<向量获取 T**<T>>
- 从 std::map 中的向量获取数据
- 从节点数组和边缘向量获取所有子树的最有效方法是什么
- C++在尝试编写对象读取器时,在尝试从一个向量获取数据时出错
- C++:向量获取意外数据
- 无法从另一个向量内的向量获取数据
- 如何从 unique_ptr 向量获取派生迭代器<Base>?