2012-07-19 3 views
4

아래의 코드는 정말 부스트 MPL 라이브러리의 이해하지 못하는 행동 재생하기 :`mpl :: plus <mpl :: int_ <1>, mpl :: int_ > :: type`은`mpl :: int_ <3>`과 같은 타입이 아닙니까?

내가 가진 질문은
#include <boost/type_traits/is_same.hpp> 
#include <boost/mpl/int.hpp> 
#include <boost/mpl/plus.hpp> 

using namespace boost; 

int main() { 
    typedef mpl::int_<1> one; 
    typedef mpl::int_<2> two; 
    typedef mpl::int_<3> three; 
    // The following line breaks compilation... 
    // static_assert(is_same< mpl::plus<one,two>::type, three >::type::value, "Not the same type"); 
    // ...while this works 
    static_assert(mpl::plus<one,two>::type::value == three::value , "Not the same value"); 
    return 0; 
} 

을 : mpl::plus<one,two>::typethree와 같은 형태가 아닌 이유는 무엇입니까? C++ Template Meta-Programming의 제 3 장 마지막에 연습을 해결하는 동안

나는이 문제에 달렸다. 나는 이미 <boost/mpl/plus.hpp>을 들여다 보았지만 거기에는 그 내용이 포함되어 있지만 코드가 너무 복잡해서 이해할 수 없었다.

답변

5

반환 유형 plus은 정수 상수 일 수 있습니다. 정확한 타입에 대한 보장이 없으므로, 어설트 이 실패 할 수 있습니다.

정확한 유형이 있습니다 :

mpl::plus<one,two>::type == mpl_::integral_c<int, 3> 
three == foo<mpl_::int_<3> > 

이 직관적이다. 한 가지 문제는 이론적으로 plus<int_, int_>integral_c을 반환 할 수 있으며 여기서 첫 번째 인수는 더 큰 용량을 가지며 오버플로의 경우 int_입니다. 유용 할 수있는 type printer 디버깅

:

template<typename> print; // undefined instantiation leads to error with name 
print<three> x; // nice error message 
+0

덕분에'템플릿 구조체 인쇄;'팁! – Massimiliano

+0

@Massimiliano'mpl :: print'도 있지만, 적어도'gcc'에서는 문제가있는 것으로 보입니다. 컴파일을 중단하지 않는 것이 훨씬 더 좋을 것입니다. – pmr

+0

을 sidenote로 사용하기 위해이 si는 부스트 Trac에 문제로 등록되었습니다. 그래서 우리는 그것이 문제라는 것을 압니다 :) –

관련 문제