Search code examples
c++templatesmetaprogrammingboost-mpl

Why does type knowledge disappear with Boost::MPL?


I have the following code and it works fine.

#include <boost\mpl\vector.hpp>
#include <boost\mpl\fold.hpp>
#include <boost\mpl\for_each.hpp>
#include <boost\mpl\inherit.hpp>
#include <boost\mpl\inherit_linearly.hpp>
#include <iostream>

using namespace boost::mpl::placeholders;

typedef boost::mpl::vector<short[2], long, char*, int> member_types;

template <typename T>
struct wrap
{
    T value;
};

struct print
{
    template <typename T>
    void operator()(T) const
    {
        std::cout << typeid(T).name() << std::endl;
    }
};

typedef boost::mpl::inherit_linearly<member_types, boost::mpl::inherit<wrap<_2>, _1> >::type Generate;

void main()
{
    Generate generated;
    print p;

    std::cout << static_cast<wrap<int>&>(generated).value << std::endl;

    boost::mpl::for_each<member_types>(p);
}

but if I modify it like this:

struct print
{
    Generate generated;
    template <typename T>
    void operator()(T) const
    {
        std::cout << static_cast<wrap<int>&>(generated).value << std::endl;
    }
};

I get the error error C2440: 'static_cast' : cannot convert from 'const Generate' to 'wrap &' with [ T=int ]

Why does it work in main, but not if I put it into a module? How can I get the data into a place I can use the value of the data created by a typelist to be called by a sequence of template functions driven by a type list. Basically how do I make an object that does something useful with the two parts?


Solution

  • If you change the operator() in print to the following, probably the code can be compiled:

    struct print {
        ...
        void operator()(T) // remove const
    

    or

    static_cast<wrap<int>const&>(generated) // add const