
Ahh.... found it. Just a typo
template<int OFFSET> struct B { typedef AA T; // ! Must be BB... enum O { offset = OFFSET }; enum S { size = 4 }; };
Sorry for wasting your time On Sat, Jan 7, 2012 at 12:55 PM, Allan Nielsen <a@awn.dk> wrote:
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; }