Boost::mpl转换操作元函数参数错误
boost::mpl transform operation meta-function argument error
在Boost MPL web文档中,它讨论了将元函数类作为Boost:: MPL::transform的参数传递。在这种情况下,元函数参数应该是在mpl::ForwardSequence上执行的某种操作。但是,当使用简单的元函数类将mpl::transform应用到mpl::map时,我得到了模板错误。(由于这些错误相当广泛,我只包括了我认为相关的部分。如果需要,我很乐意发布更广泛的错误报告。
错误:/usr/include/boost/mpl/aux_/preprocessed/gcc/bind.hpp:207:21: error: no type named ‘type’ in ‘struct boost::mpl::apply_wrap2<boost::mpl::push_front<mpl_::na, mpl_::na>, boost::mpl::map0<>, boost::mpl::pair<unsigned int, INT32U> >’
test_boost_mpl.cpp:106:1: error: ‘from_native_tmap’ was not declared in this scope
在我的特殊情况下,我的代码如下所示:
/* stl includes */
#include <cstdint>
/* boost includes */
#include <boost/type_traits.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/transform.hpp>
struct Boolean {
enum { tag_value = 0x83 };
};
struct INT32U {
enum { tag_value = 0x84 };
};
typedef mpl::map
<
mpl::pair<Boolean, bool>,
mpl::pair<INT32U, std::uint32_t>
> to_native_tmap;
struct swap_f {
template<typename PAIR>
struct apply {
typedef typename mpl::pair<typename PAIR::second, typename PAIR::first> type;
};
};
typedef mpl::transform<to_native_tmap, swap_f>::type from_native_tmap;
BOOST_MPL_ASSERT(( is_same
<mpl::at<from_native_tmap, bool>::type, Boolean> ));
BOOST_MPL_ASSERT(( is_same
<mpl::at<from_native_tmap, std::uint32_t>::type, INT32U> ));
int main(void) { return 0; }
我的意图是在to_native_tmap
中映射到本地c++类型,然后在from_native_tmap
中反向映射。
此代码在BOOST_MPL_ASSERT()
上失败,或者如果我尝试实例化to_native_tmap
mpl::map类型。
大大的"谢谢你!"
/* stl includes */
#include <cstdint>
/* boost includes */
#include <boost/type_traits.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/inserter.hpp> //ADDED
#include <boost/mpl/insert.hpp> //ADDED
namespace mpl= boost::mpl; //ADDED
using boost::is_same; //ADDED
struct Boolean {
enum { tag_value = 0x83 };
};
struct INT32U {
enum { tag_value = 0x84 };
};
typedef mpl::map
<
mpl::pair<Boolean, bool>,
mpl::pair<INT32U, std::uint32_t>
> to_native_tmap;
struct swap_f {
template<typename PAIR>
struct apply {
typedef typename mpl::pair<typename PAIR::second, typename PAIR::first> type;
};
};
//The default inserter used by mpl::transform requires that the container have a push_front algorithm
//This isn't the case for mpl::map (as the assertion in g++4.8.0 reveals: REQUESTED_PUSH_FRONT_SPECIALIZATION_FOR_SEQUENCE_DOES_NOT_EXIST)
//In this case you need to add a custom inserter
typedef mpl::inserter<mpl::map0<>, mpl::insert<mpl::_1,mpl::_2> > map_inserter; //ADDED
typedef mpl::transform<to_native_tmap, swap_f, map_inserter >::type from_native_tmap; //CHANGED
BOOST_MPL_ASSERT(( is_same
<mpl::at<from_native_tmap, bool>::type, Boolean> ));
BOOST_MPL_ASSERT(( is_same
<mpl::at<from_native_tmap, std::uint32_t>::type, INT32U> ));
int main(void) { return 0; }
相关文章:
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 当从函数参数中的临时值调用复制构造函数时
- 如何从"decltype()"获取函数参数的数量<funtion>?
- 如何将lambda作为模板类的成员函数参数
- 模板参数推导失败,函数参数/参数不匹配
- 如何在C++中将迭代器作为函数参数传递
- 将函数参数"const char*"转换为"std::string_view"是
- C++ 如何将数组值解压缩为函数参数
- 主函数参数的属性
- 具有两个间接寻址运算符 (C++) 的函数参数的用途
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何定义在用作函数参数时工作的类模板的转换
- 将函数参数完美转发到函数指针:按值传递呢?
- 为什么我不能将引用作为 std::async 的函数参数传递
- 什么..(省略号)作为函数原型中唯一的函数参数,C++?
- 是否可以就地构造一个固定大小的数组作为函数参数?
- 接受模板作为函数参数
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- Arduino 函数参数