boost 공부중인데 이 코드가 왜 컴파일이 가능한건지 궁금합니다.
글쓴이: cpuz / 작성시간: 목, 2009/02/05 - 4:38오후
template(typename KeyType, typename ValueType) class DataType { public: DataType() {} DataType(const KeyType& key, const ValueType& value) : key(key), value(value) {} int compare(const DataType& other) const { return _compare(other, static_cast(boost::is_arithmetic(KeyType)*)(0)); } private: int _compare(const DataType& other, const boost::mpl::true_*) const { return key-other.key; } int _compare(const DataType& other, const boost::mpl::false_*) const { return key.compare(other.key); } public: KeyType key; ValueType value; };
여기서 return key.compare(other.key); 부분입니다.
key 에는 compare 라는 함수가 없습니다.
key 자체가 템플릿 타입으로 KeyType 이거든요.
boost 의 메타프로그래밍 때문에 이것이 컴파일이 가능한 것 같기도 하고...
위 코드의 로직은 이해하겠는데,
return key.compare(other.key);
이것이 왜 문제가 없는 코드인지 궁금합니다 ㅠ
P.S : 템플릿 < > 기호가 표시가 안되길래 ( ) 로 바꿨습니다.
Forums:
템플릿코드
템플릿코드 자체만으로는 컴파일되는지 안되는지 알수 없습니다.
실제로 템플릿 사용될때, KeyType으로 지정된 타입이, compare 멤버함수를 가지고 있다면 컴파일이 될것이고, 없다면 에러가 날것입니다.
댓글 달기