Discussion:
[Boost-users] [mpl] Working with map, copy and back_inserter
☂Josh Chia (謝任中)
2017-02-05 09:42:15 UTC
Permalink
I'm trying to implement a compile-time topological sort of the DAG
representing a certain dependency relationship between my classes.

One of the data structures I can use to implement topological sort is a
map, so I'm trying to use mpl::map using mpl::size_t<> as key. The
documentation doesn't define key equality for mpl::map, but I assume it's
effectively std::is_same<>. If so, I think I have a problem. In the
following code, the last two lines fail static_assert().

The essence of the problem is that my attempt to construct/build my keys
using copy with back_inserter does not give me a mpl::vector<> of
mpl::size_t<> that is_same to Count, the expected value. It does give me a
sequence, and the elements have the right value and value_type but they are
not is_same to the expected size_t<> types. So, if I use the elements
together with the size_t<> in an mpl::map, won't they be considered to be
different keys?

Josh

#include <type_traits>

#include <boost/mpl/at.hpp>
#include <boost/mpl/back_inserter.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/size_t.hpp>
#include <boost/mpl/vector.hpp>

using namespace std;
namespace mpl = boost::mpl;

using Empty = typename mpl::vector<>;
using Count = mpl::vector<mpl::size_t<0>, mpl::size_t<1>>;
using ConstructedCount = typename mpl::copy<
typename mpl::range_c<size_t, 0, 2>, mpl::back_inserter<Empty>>::type;
using The0 = typename mpl::at<ConstructedCount, mpl::size_t<0>>::type;
using The1 = typename mpl::at<ConstructedCount, mpl::size_t<1>>::type;

static_assert(mpl::equal<Count, ConstructedCount, mpl::equal_to<mpl::_1,
mpl::_2>>::value, "succeeds");
static_assert(The1::value == 1, "succeeds");
static_assert(std::is_same<typename The1::value_type, size_t>::value,
"succeeds");

static_assert(std::is_same<Count, ConstructedCount>::value, "fails");
static_assert(std::is_same<The1, mpl::size_t<1>>::value, "fails");
Steven Watanabe
2017-02-05 20:40:37 UTC
Permalink
AMDG
<snip>
The essence of the problem is that my attempt to construct/build my keys
using copy with back_inserter does not give me a mpl::vector<> of
mpl::size_t<> that is_same to Count, the expected value. It does give me a
sequence, and the elements have the right value and value_type but they are
not is_same to the expected size_t<> types. So, if I use the elements
together with the size_t<> in an mpl::map, won't they be considered to be
different keys?
Yes. If you want to use a map this way, you'll
need to normalize the keys for insertion.

something like this should do the trick:

template<class T>
struct as_size_t { typedef mpl::size_t<T::value> type; };

In Christ,
Steven Watanabe

Loading...