Skip to main content

Posts

Showing posts with the label foreach

C++ of the Day #6 - A way to get the type of an expression without evaluating the expression

오늘은 The C++ Source 에서 글을 읽다가 발견한 멋진 테크닉 하나 소개합니다. 바로 아래 나와 있는 코드입니다. (( Conditional Love: FOREACH Redux )) // a simple type wrapper template<class T> struct type2type {}; // convert an expression of type T to an // expression of type type2type template<class T> type2type<T> encode_type(T const & t) { return type2type<T>(); } // convertible to type2type for any T struct any_type { template<class T> operator type2type<T> () const { return type2type<T>(); } }; // convert an expression of type T to an // expression of type type2type without // evaluating the expression #define ENCODED_TYPEOF( container ) ( true ? any_type() : encode_type( container ) ) 위의 코드에서 가장 멋진 부분은 바로 가장 아래 줄의 ternary conditional operator (?:) 를 사용한 부분입니다. 이 코드에서 원하는 것은 매크로의 container 부분을 evaluation하지 않고 container의 타입만을 다음 template 함수에 전달하는 것이며 여기서는 type2type<container> 타입의 형태가 전달되기를 원합니다. 물론 여기서 encode_type(container) 를 바로 사용하면 container 부분이 eval...