Boost logo

Boost Users :

Subject: [Boost-users] Register variadic template as tags in a boost::mpl::map
From: Allan Nielsen (a_at_[hidden])
Date: 2012-01-07 06:55:04


In the following code, why can't I look up BB and CC in the mpl::map type_map?

I would expect the map to look like this:

map <
       pair<AA, A< xx > >,
       pair<BB, B< xx > >,
       pair<CC, C< xx > > >

But it does not seem like this is the case...

#include <iostream>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/insert.hpp>

struct AA {};
struct BB {};
struct CC {};

template<int OFFSET>
struct A {
    typedef AA T;
    enum O { offset = OFFSET };
    enum S { size = 2 };
};

template<int OFFSET>
struct B {
    typedef AA T;
    enum O { offset = OFFSET };
    enum S { size = 4 };
};

template<int OFFSET>
struct C {
    typedef AA T;
    enum O { offset = OFFSET };
    enum S { size = 4 };
};

template < template <int> class Head,
           template <int> class... Tail>
struct X : public Head< X<Tail...>::size * X<Tail...>::offset >,
           public X<Tail...>
{
    typedef Head< X<Tail...>::size * X<Tail...>::offset > _Head;

    enum O { offset = X<Tail...>::size * X<Tail...>::offset };
    enum S { size = _Head::size * X<Tail...>::size };

    typedef typename boost::mpl::insert < typename X<Tail...>::type_map,
                                          boost::mpl::pair<typename _Head::T,
                                                           _Head >
>::type type_map;

    template <typename T> int get_offset() {
        return boost::mpl::at<type_map, T>::type:: offset;
    }
};

// base case
template < template <int> class Head>
struct X<Head> : public Head<1> {
    enum O { offset = Head<1>::offset };
    enum S { size = Head<1>::size };

    typedef boost::mpl::map< boost::mpl::pair< typename Head<1>::T,
                                               Head<1> > > type_map;
};

int main()
{
    X< A, B, C > x1;

    std::cout << x1.get_offset<CC>() << std::endl;

    // generates compile errors
    //std::cout << x1.get_offset<BB>() << std::endl;
    //std::cout << x1.get_offset<CC>() << std::endl;

    return 0;
}


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