간단한 참고 사항: C++11 템플릿을 사용하여 이름과 유형별로 구조체 필드를 감지하고 정적으로 분기할 수 있습니다. 저는 이 솔루션을 breeze1990에서 처음 들었습니다.
구조체의 필드 크기가 int 유형인지 감지하고 싶다고 가정해 봅시다. 같은 이름의 템플릿 인스턴스화 두 개를 생성하고, 기본값은 거짓으로 설정된 HasStaticSize를 생성합니다.
#include template struct HasStaticSize : std::false_type {}; template struct HasStaticSize< T, typename std::enable_if< std::is_same>::value, void>::type> : std::true_type {}; 후자는 T::size가 int로 선언된 경우, 더 구체적으로 int로 "붕괴"되는 경우에만 해결됩니다.
jeremykun.com
Detecting field names with C++ metaprogramming
Create attached notes ...
