boost 공부중인데 이 코드가 왜 컴파일이 가능한건지 궁금합니다.

cpuz의 이미지

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 : 템플릿 < > 기호가 표시가 안되길래 ( ) 로 바꿨습니다.

klara의 이미지

템플릿코드 자체만으로는 컴파일되는지 안되는지 알수 없습니다.
실제로 템플릿 사용될때, KeyType으로 지정된 타입이, compare 멤버함수를 가지고 있다면 컴파일이 될것이고, 없다면 에러가 날것입니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.