1

私は次のコードを持っています、そしてそれはうまくいきます。

#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);
}

しかし、私がこのように変更すると:

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

エラーエラーC2440が発生します:'static_cast':'const Generate'から'wrap&'に[T=int]で変換できません

メインで動作するのに、モジュールに入れても動作しないのはなぜですか?タイプリストによって作成されたデータの値を使用して、タイプリストによって駆動される一連のテンプレート関数によって呼び出される場所にデータを取得するにはどうすればよいですか。基本的に、2つの部分で何か役立つオブジェクトを作成するにはどうすればよいですか?

4

1 に答える 1

3

を次のように変更するoperator()print、おそらくコードをコンパイルできます。

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

また

static_cast<wrap<int>const&>(generated) // add const
于 2011-05-01T12:28:02.420 に答える