Ошибка аргумента мета-функции операции boost :: mpl

в Повышение MPL веб-документации, в нем говорится о передаче классов мета-функций в качестве аргументов для 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; }

Мое намерение состоит в том, чтобы иметь отображение на нативные типы C ++ в to_native_tmap а затем обратное отображение в from_native_tmap,

Этот код не работает ни на BOOST_MPL_ASSERT() или если я попытаюсь создать экземпляр to_native_tmap mpl :: тип карты

Большое спасибо!’ впереди всех, кто готов помочь.

2

Решение

/* 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; }
5

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector