Boost logo

Boost Users :

Subject: [Boost-users] [mpl] at<> behaves differently for vector_c<> and vector<>
From: 謝任中 (joshchia_at_[hidden])
Date: 2017-02-05 11:09:51


I asked earlier in a post "[mpl] Working with map, copy and back_inserter".

This is a related question. The common question is: What does compile-time
equality means in MPL, particularly for map and set key equality? Does it
mean std::is_same? The following code works whether USE_VECTOR_C is defined
as 0 or 1 -- the last 2 lines are static_asserts that have conditions
determined by USE_VECTOR_C so that they succeed, highlighting the
difference in behavior between USE_VECTOR_C == 0 and USE_VECTOR_C == 1.
USE_VECTOR_C == 0 is the normal case where everything is working as I
expect. USE_VECTOR_C == 1 is the unexpected case where the static_asserts
in the last 2 lines have conditions altered so that they succeed.

Why do vector_c and vector give me different "element values"? In general,
what should I be careful about when trying to use the "compile-time values"
as keys to avoid the situation elucidated by the last line, where I expect
two "compile-time values" to be the same but they are not, resulting in two
set/map items instead of one?

To clarify, I'm using boost 1.63 with gcc 6.3.1 and -std=c++14 on Arch
Linux.

Josh

#include <type_traits>

#include <boost/mpl/at.hpp>
#include <boost/mpl/insert.hpp>
#include <boost/mpl/set.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/size_t.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/vector_c.hpp>

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

#define USE_VECTOR_C 0

using Zero = mpl::size_t<0>;
#if USE_VECTOR_C == 1
using Vector = mpl::vector_c<size_t, 0>;
#else
using Vector = mpl::vector<Zero>;
#endif

using ZeroElt = typename mpl::at<Vector, Zero>::type;
using EmptySet = mpl::set<>;
using NonEmptySet = typename mpl::insert<typename mpl::insert<EmptySet,
Zero>::type, ZeroElt>::type;

static_assert(ZeroElt::value == 0, "");
static_assert(is_same<ZeroElt::value_type, size_t>::value, "");
static_assert(is_same<ZeroElt, Zero>::value == !USE_VECTOR_C, "");
static_assert(mpl::size<NonEmptySet>::value == USE_VECTOR_C + 1, "");



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net