☂Josh Chia (謝任中)
2017-02-05 11:09:51 UTC
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
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>;
using Vector = mpl::vector<Zero>;
using ZeroElt = typename mpl::at<Vector, Zero>::type;
using EmptySet = mpl::set<>;
using NonEmptySet = typename mpl::insert<typename mpl::insert<EmptySet,
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, "");