2017-02-24 11 views
3

고려 :std :: is_arithmetic은 int 유형에 대해 일반 람다 내부에 false를 반환합니다. 정의되지 않은 동작입니까?

#include <iostream> 
#include <typeinfo> 
#include <type_traits> 

#include <cxxabi.h> 

#include <boost/hana.hpp> 
namespace hana = boost::hana; 

struct Person { 
    BOOST_HANA_DEFINE_STRUCT(Person, 
    (std::string, name), 
    (int, age) 
); 
}; 

template<typename T> 
void stringify(const T& v) { 
    hana::for_each(hana::accessors<T>(), [&v](auto a) { 
     // Here I'm printing the demangled type, just to make sure it is actually the type I'm thinking it is. 
     std::cout << abi::__cxa_demangle(typeid(decltype(hana::second(a)(v)){}).name(), 0, 0, 0); 

     // If the value is arithmetic, "quote" should be an empty string. Else, it should be an actual quote. 
     // UNEXPECTED BEHAVIOR IS HERE 
     std::string quote{(std::is_arithmetic<decltype(hana::second(a)(v))>::value?"":"\"")}; 

     // Finally do what we're here for. 
     std::cout << " " << hana::first(a).c_str() << " = " << quote << hana::second(a)(v) << quote << "\n"; 
    }); 
} 

int main() { 
    Person john; 
    john.name = "John Doe"; 
    john.age = 42; 
    stringify(john); 
} 

See it live

출력 : 내가 대신 다른 비 산술 유형의 수를 처리하고있어 경우에 알려 std::is_arithmetic를 사용하기 위해 노력하고있어

std::__cxx11::basic_string</*...*/> name = "John Doe" 
int age = "42" 

그에 따라 따옴표를 인쇄 (또는 인용하지 않음)하십시오.

그러나

는, 값이 ( ::value 부재를 통해) "반환"되는 어떤 이유로 int이 (내가 사용하여 분해 해제 유형을 인쇄 처음으로 GCC의 cxxabi.h 그 권리를하고 있어요 확인도 내가 전달 해요하지만, false입니다)

출력에서 ​​볼 수 있듯이 int은 따옴표로 인쇄됩니다.

내 질문은 다음과 같습니다. 왜 잘못된 값을 반환합니까? 이것은 일반적인 람다와 관련이 있습니까? 고칠 수 있을까요?

실제로이 파일을 Coliru에서 직접 테스트 중이므로 gcc 버전이 사용 된 것으로 가정 할 수 있습니다 (현재 6.3.0).

+0

http://coliru.stacked-crooked.com/a/6d50720bf7bf64aa – llonesmiz

+0

@llonesmiz oohhh. 나는 어딘가에서 내가 바보가되어야한다는 것을 알았다. 이 질문을 받아 들일 수 있도록 답변으로 게시 할 수 있습니까? –

+0

"demangled type"을 인쇄하는 경우, 좋은 간단한 간단한 대안은 유효하지 않은 초기화로 변수 선언을 추가하여 진단의 일부로 유형 설명을 생성하는 것입니다. :) –

답변