|
Boost Users : |
Subject: Re: [Boost-users] Register variadic template as tags in a boost::mpl::map
From: Allan Nielsen (a_at_[hidden])
Date: 2012-01-07 07:08:47
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_at_[hidden]> 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;
> }
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