2011-05-01 3 views
1

다음 코드가 있는데 제대로 작동합니다.Boost :: MPL로 왜 형식 지식이 사라지나요?

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

하지만 난 이런 식으로 수정할 경우 :

나는 오류 오류 C2440 얻을
struct print 
{ 
    Generate generated; 
    template <typename T> 
    void operator()(T) const 
    { 
     std::cout << static_cast<wrap<int>&>(generated).value << std::endl; 
    } 
}; 

: 'static_cast': [ 와 '& 랩'에 'const를 생성'에서 을 변환 할 수 있습니다 T = int

왜 메인에서 작동 하나, 모듈에 끼워 넣지는 않습니까? 유형 목록에 의해 구동되는 일련의 템플리트 함수에 의해 호출 될 유형 목록에 의해 작성된 데이터 값을 사용할 수있는 곳으로 데이터를 가져올 수 있습니까? 기본적으로 두 부분으로 유용한 무언가를 만드는 객체를 만드는 방법은 무엇입니까? 당신이 다음에 printoperator()을 변경하는 경우

+0

http://stackoverflow.com/questions/5790161/is -the-back-in-c-and-c-include-directives – fazo

답변

3

은, 아마도 코드를 컴파일 할 수 있습니다 :

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

또는

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

절대적으로 환상적입니다! 당신은 내가 그 머리로 정말 내 머리를 긁적 거리고 있었다고 상상할 수 있습니다. 다시 한번 감사드립니다. – Tavison

+0

당신은 환영합니다 :-) –

관련 문제