Why the extension of the mpl set allows non-unique types

I believe that my understanding of boost :: mpl :: set should be fundamentally wrong. I thought this only allowed unique types.

But the following code compiles:

#include <boost/mpl/set.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/accumulate.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/sizeof.hpp>
#include <boost/mpl/assert.hpp>
using namespace boost::mpl;

typedef set<long,float,long> my_set; //long repeated in set?
typedef vector<long,float,long> my_vec; //seems reasonable

typedef accumulate<
  my_set
  , int_<0>
  , plus<_1, sizeof_<_2>>
  >::type set_size;

typedef accumulate<
  my_vec
  , int_<0>
  , plus<_1, sizeof_<_2>>
  >::type vec_size;

BOOST_MPL_ASSERT_RELATION( vec_size::value, ==, sizeof(long)+sizeof(float)+sizeof(long) );
//why does the following line compile?
//shouldn't the size be sizeof(long)+sizeof(float) instead?
BOOST_MPL_ASSERT_RELATION( set_size::value, ==, sizeof(long)+sizeof(float)+sizeof(long) );
+5
source share
1 answer

Take another look at the documentation .

The list of types T1, T2, T3, ..., TNused to build the set must not contain duplicates. (Or else - the design set<T1, T2, ..., TN>has only a certain value, if it T1, T2, T3, ..., TNdoes not contain duplicates).

Later in the documentation there is an example of how to build a set from a list of elements that may contain duplicates:

typedef fold<
  vector<long,float,long>
, set0<>
, insert<_1,_2>
>::type s;

BOOST_MPL_ASSERT_RELATION( size<s>::value, ==, 2 );

, .

+10

All Articles