增强融合变换类型操作和as_vector
Boost Fusion transform type manipulation and as_vector
我试图理解使用Fusion的意义,并被这个简单的例子难住了:
#include <boost/fusion/include/is_sequence.hpp>
#include <boost/fusion/include/as_vector.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/include/transform.hpp>
template< typename T >
struct S {
typedef T type;
};
struct S_f {
template< typename T >
struct result {
typedef typename T::type type;
};
};
int main () {
using namespace boost;
typedef fusion::vector<S<int>> from_type;
BOOST_MPL_ASSERT((fusion::traits::is_sequence< fusion::vector< int > > ));
typedef fusion::result_of::transform< from_type, S_f >::type to_type;
BOOST_MPL_ASSERT((fusion::traits::is_sequence< to_type > ));
typedef fusion::result_of::as_vector< to_type >::type value_type; // error
}
断言通过,但 value_type 的类型定义失败,并显示以下错误。我无法资助代码和文档之间的任何差异,也无法在堆栈溢出或提升邮件列表的其他地方采取补救措施。
AFAICT 代码是正确的:应用转换元函数的结果是一个transform_view,transform_view是一个序列,如传递断言所示。然而,as_vector元函数在transform_view上的应用失败了。什么给?!
任何帮助,不胜感激。我对混合 mpl 不感兴趣。我知道我可以绕道 MPL,SO 上的一些关于类型操作的融合问题都有提倡 MPL 的答案。根据文档,我不需要MPL。
clang++ -std=c++1z -c t.cpp
In file included from main.cpp:4:
In file included from /usr/local/include/boost/fusion/include/transform.hpp:11:
In file included from /usr/local/include/boost/fusion/algorithm/transformation/transform.hpp:11:
In file included from /usr/local/include/boost/fusion/view/transform_view/transform_view.hpp:15:
In file included from /usr/local/include/boost/fusion/view/transform_view/transform_view_iterator.hpp:18:
/usr/local/include/boost/fusion/view/transform_view/detail/value_of_impl.hpp:37:74: error: no type named 'type' in 'boost::mpl::apply<boost::fusion::detail::apply_transform_result<S_f>, S<int>, mpl_::na, mpl_::na, mpl_::na, mpl_::na>'
typedef typename mpl::apply<transform_type, value_type>::type type;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/usr/local/include/boost/fusion/iterator/value_of.hpp:52:15: note: in instantiation of template class 'boost::fusion::extension::value_of_impl<boost::fusion::transform_view_iterator_tag>::apply<boost::fusion::transform_view_iterator<boost::fusion::vector_iterator<boost::fusion::vector<S<int>, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_>, 0>, S_f> >' requested here
: extension::value_of_impl<typename detail::tag_of<Iterator>::type>::
^
/usr/local/include/boost/fusion/container/vector/detail/cpp03/preprocessed/as_vector10.hpp:19:49: note: in instantiation of template class 'boost::fusion::result_of::value_of<boost::fusion::transform_view_iterator<boost::fusion::vector_iterator<boost::fusion::vector<S<int>, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_>, 0>, S_f> >' requested here
typedef typename fusion::result_of::value_of<I0>::type T0;
^
/usr/local/include/boost/fusion/container/vector/convert.hpp:26:17: note: in instantiation of template class 'boost::fusion::detail::barrier::as_vector<1>::apply<boost::fusion::transform_view_iterator<boost::fusion::vector_iterator<boost::fusion::vector<S<int>, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_>, 0>, S_f> >' requested here
template apply<typename result_of::begin<Sequence>::type>::type
^
main.cpp:26:32: note: in instantiation of template class 'boost::fusion::result_of::as_vector<boost::fusion::transform_view<boost::fusion::vector<S<int>, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_>, S_f, boost::fusion::void_> >' requested here
typedef fusion::result_of::as_vector< to_type >::type value_type; // error
^
1 error generated.
模板元编程的问题在于,当你不能满足元函数的前提条件时,你会得到很多无意义的错误。对transform
的要求是F
是一元多态函数对象。文档中对内容的解释有点弱,但您可以从示例中看出:这是一个可以使用参数调用的对象。也就是说,result_of<F(T)>::type
需要格式良好。
您要传递给transform
的是:
struct S_f {
template< typename T >
struct result {
typedef typename T::type type;
};
};
这不是多态函数对象。它也不是元函数类。这不是Boost.Fusion和Boost.MPL能够理解的。特别令人困惑的是,transform<>
元函数是惰性的 - 所以看起来你正确地完成了这部分。只有在as_vector<>
中才实际应用转换,因此看起来这就是失败点所在。
若要将其转换为多态函数对象,只需将嵌套的result
类模板更改为调用运算符:
struct S_f {
template< typename T >
typename T::type operator()(T );
};
不需要定义,因为你实际上并没有调用它。通过该修复,您的代码将编译。
相关文章:
- 有关插入适配器的错误。[错误]请求从 '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比数组慢
- "lvalue required as increment operand" 关于 C++ 和 stl vector's begin()
- 如何使用 std::vector<std::tuple<A,B>> 来管理内存(调整大小、保留,...),但实际上将 As 保留在 B 之前,连续
- 将 vector<unsigned char> {1,2,3} 转换为字符串"1-2-3" AS DIGITS
- Serilize List<List> <string>in ProtoBuf-net 和 Deserialize as a C++ vector<vector<s