c++ Boost MPL:如何摆脱vector和callnot内部函数
C++ Boost MPL: how to get rid of vector and callnot internal function?
我正在学习Boost。MPL和我才刚刚开始。如果解决方案很明显,请原谅我。我看这样的例子:
#include <boost/mpl/vector.hpp>
#include <boost/mpl/for_each.hpp>
#include <iostream>
using namespace std;
struct A
{
template <class T>
void operator()(T t)
{
cout << typeid(T).name() << "t" << t << endl;
}
template <class TypeVector>
void FooAll(void)
{
boost::mpl::for_each<TypeVector>(*this);
}
};
void main(void)
{
A a;
a.FooAll<boost::mpl::vector<int, float, long>>();
}
和不禁想知道如何摆脱boost::mpl::vector
时,调用FooALL(把它变成a.FooAll<int, float, long>();
),并为每个参数调用一些静态/全局/或类内部函数,而不是混淆我的*this
?
请查看boost元组实现(解决类似问题)。其主要思想是,您可以为您的FollAll<…>()方法指定最大数量的模板参数,并为大多数模板参数提供默认类型。这是我脑海中的一个草图
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/push_back.hpp>
using boost::is_same;
using boost::mpl::eval_if;
using boost::mpl::vector;
using boost::mpl::push_back;
struct EmptyType { };
struct A
{
template<typename arg1, typename arg2=EmptyType, typename arg3=EmptyType, ..., typename argN=EmptyType>
void FooAll() {
// reconstruct the type vector for easy manipulation later
// Bolierplate code!
typedef vector<arg> vector_arg1;
typedef typename eval_if<is_same<arg2, EmptyType>,
vector_arg1,
push_back<vector_arg1, arg2> >::type vector_arg2;
typedef typename eval_if<is_same<arg3, EmptyType>,
vector_arg2,
push_back<vector_arg2, arg3> >::type vector_arg3;
//... rest of arguments
typedef typename eval_if<is_same<argN, EmptyType>,
vector_arg(N-1),
push_back<vector_arg(N-1), argN> >::type vector_argN;
// now you can manipulate the reconstructed type vector
Do_some_internal_stuff<vector_argN>::apply();
}
}
如果你想要"高科技",你可以尝试c++ 11的一个标准特性——可变模板。但是请确保您的目标编译器已经支持此特性。
最诚挚的问候,Marcin
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中用vector填充一个简单的动态数组
- vector.resize()中的分配错误
- 使用std::vector的OpenCL矩阵乘法
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 在某些循环内使用vector.push_back时出现分段错误
- 当vector是tje全局变量时,c++中vector的内存管理
- std::vector的包装器,使数组的结构看起来像结构的数组
- 为什么(-1)%vector::size()总是返回0
- 在C++中将类(带有Vector成员)保存为二进制文件
- 编译器如何区分std::vector的构造函数
- 将 int 数组转换为 std::vector<int*>
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 在std::vector上存储带有模板的类实例
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 为什么std::vector比数组慢
- std::vector::迭代器是否可以合法地作为指针
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- c++ Boost MPL:如何摆脱vector和callnot内部函数