typename과 class의 차이점이 먼가요?

junteken의 이미지

현재 C++ in depth를 공부하고 있는 학생입니다.
Modern C++ design책을 보고있는데요...
여기보면 typename과 class를 template에서 인자로 쓰는데요..
제가알기로는 typename과 class를 따로 쓰고있는 예제 코드가
눈에 띕니다...제가 잘못알고있는건가요? 이 두개가 다른건가요?
어떻게 다른지 알려주시면 감사하겠습니다.
그럼 행복하세요~~~

doldori의 이미지

템플릿의 type parameter를 쓸 때는 어느 것을 쓰든 마찬가지입니다.
type parameter가 꼭 클래스형일 필요는 없기 때문에 class보다는
typename을 선호하긴 합니다.

cdpark의 이미지

<class T> 가 class String을 받으면 그러려니 하는데, int도 받고, long도 받으니 좀 애매하죠. 그래서 이왕 예약해놓은 단어(새로 예약(!)한 건가요?)인 typename을 쓰는거죠.

yielding의 이미지

typename 키워드는 3가지로 사용이 됩니다.

 
template <typename T> -------------- (1)
class MyClass {
    typename T::SubType *ptr;  ------- (2)
    ...
};

첫번 째 경우는 위 코드의 (1)에 해당하며 의 위 글들에서 잘 설명해주셨고요
두번 째 경우는 (2) typename 이 없으면 T::SubType에서 Subtype이
class T의 static member인지 아니면 클래스 내부에 정의된
타입인지 알 수 없습니다.. static member라면 ptr과 곱하기가
되겠죠..

그리고 마지막은 명예의 전당 bit값 뒤집기의 글타래 중 std::bitset의 to_string
에 관련된 댓글에 설명이 되어있습니다. (less than operator와 template 다음에 나오는 시작의 꺽쇠 '<' 를 구분하기 위해..)

Life rushes on, we are distracted

cdecl의 이미지

yielding님의 마지막 예의 보충 설명을 드리자면 ...

그러한 문법을 Member template specialization 이라고 합니다.
멤버 template함수의 전문화(특수화)를 표현하기위해 고안된 문법입니다.

제가 테스트한 결과로는
VC6 : 에러를 냅니다.
VC7.1 : 넣어도되고 안넣어도 됩니다.
g++ 은 아직 확인 해보지 못했습니다.

14.2.4
When the name of a member template specialization appears after . or ->
in a postfix-expression, or after :: in a qualified-id that explic-
itly depends on a template-argument (_temp.dep_), the member template
name must be prefixed by the keyword template. Otherwise the name is
assumed to name a non-template. [Example:

class X {
public:
	template<size_t> X* alloc();
	template<size_t> static X* adjust();
};
void f(X* p)
{
	X* p1 = p->alloc<200>();
	// ill-formed: < means less than
	
	X* p2 = p->template alloc<200>();
	// fine: < starts explicit qualification
	T::adjust<100>();
	// ill-formed: < means less than
	T::template adjust<100>(); 
	// fine: < starts explicit qualification
}

--end example]

[Programming Language C++ (ISO)]

그리고 Herb Sutter가 template의 파라미터로서 즐겨쓰는 구분 법은 사용자정의 타입 즉 class 만 적용될수 있으면 class를, 일반적인 native타입도 같이 적용 될수 있으면 typename을 쓰고 있습니다.

개인적으로 괜찮은 표기법이라 생각이 듭니다.
물론 개인적인 취향이겠지만 서도 ..

:wink:

--
cdecl

댓글 달기

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 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <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].
  • 사용할 수 있는 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>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <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].
  • You can use Textile markup to format text.
  • 사용할 수 있는 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>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <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].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 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>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.