Unexpected result using fighter boost mpl inserter

I expected the following to produce the same result:

namespace mpl = boost::mpl;

template<int from, int to>
struct
make_vector1
 : mpl::copy<
     mpl::range_c<int,from,to>, 
     mpl::inserter< 
       mpl::vector<>,
       mpl::push_back<mpl::placeholders::_1,
              mpl::placeholders::_2 // <- Copy int_ types
             >
       > 
     >  
{};

template<int from, int to>
struct
make_vector2 
 : mpl::copy<
     mpl::range_c<int,from,to>, 
     mpl::inserter< 
       mpl::vector<>,
       mpl::push_back<mpl::placeholders::_1,
              mpl::int_<mpl::placeholders::_2::value> // <- Alternative?
              >
       > 
     >
{};

But they do not.

int
main  (int ac, char **av)
{
  typedef make_vector1<0,3>::type v1;
  typedef make_vector2<0,3>::type v2;

  //returns 0, as I would expect
  std::cout<<"I1 = "<<mpl::at<v1,mpl::int_<0> >::type::value <<std::endl;

  //returns 2, which has me stumpted.
  std::cout<<"I2 = "<<mpl::at<v2,mpl::int_<0> >::type::value <<std::endl;
}

Any idea what is going on here?

I want to use the second method to build mpl :: vector types Example, where:

template<int i>
struct Example : mpl::int_<i>
{};

but i can't get it to work.

Many thanks

+3
source share
1 answer

You get the value 2 because :: the value on _2 is defined as 2 (placeholder index). MPL does not define :: on placeholders for an obvious reason, so you cannot do this directly.

mpl:: range_c mpl:: int_, , , . mpl .

, , mpl:: int_ . , -- , mpl:: int_ abstraction:

#include <boost/mpl/at.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/push_back.hpp>

namespace mpl = boost::mpl;

template<int I> struct Example : boost::mpl::int_<I> 
{
  static void foo() { std::cout << "**" << I << "**\n";}
};

template<class T> struct make_example
{
  typedef Example<T::value> type;
};

template<int from, int to>
struct
make_vector2 
 : mpl::copy<
     mpl::range_c<int,from,to>, 
     mpl::inserter< 
       mpl::vector<>,
       mpl::push_back<mpl::placeholders::_1,
              make_example<mpl::placeholders::_2> // <- Alternative?
              >
       > 
     >
{};

int main(int ac, char **av)
{
  typedef make_vector2<0,3>::type v2;

  mpl::at<v2,mpl::int_<0> >::type::foo();
}

foo() , .

, recap:

  • , MPL int_. int_ .
  • :: , ,
  • - an , .
+4

All Articles