1

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?

Tavison
  • 1,523
  • 1
  • 11
  • 19
  • http://stackoverflow.com/questions/5790161/is-the-backslash-acceptable-in-c-and-c-include-directives – fazo May 01 '11 at 12:36

1 Answers1

3

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
Ise Wisteria
  • 11,259
  • 2
  • 43
  • 26
  • Absolutely fantastic! You can imagine I was really scratching my head with that one. Thanks again. – Tavison May 01 '11 at 12:31