//////////////////////////////////////////////////////////////////////////////////////
//
// 템플릿 클래스 메소드 특화
//
//
//////////////////////////////////////////////////////////////////////////////////////
#include <string>

#include <windows.h>

template <class CharType>
class Message
{
public:
 void Print (void);
 void Set   (std::basic_string < CharType > s);

public:
 std::basic_string < CharType > text_;
};

template <class CharType>
void Message<CharType>::Set (std::basic_string < CharType > s)
{
 text_ = s;
}

template <class CharType>
void Message<CharType>::Print (void)
{
 OutputDebugStringW (text_.c_str());
}

template <>
void Message<char>::Print (void)
{
 printf (text_.c_str());
}

int _tmain(int argc, _TCHAR* argv[])
{
 Message<wchar_t> b;

 b.Set (L"bbb\r\n");
 b.Print();


 Message<char> a;

 a.Set ("aaa\r\n");
 a.Print();


 return 0;
}
--------------------------------------------------------------------------------------
aaa (콘솔창)
--------------------------------------------------------------------------------------
bbb (디버깅창)





//////////////////////////////////////////////////////////////////////////////////////
//
// 템플릿 클래스 특화 (Specialization of template class)
//
//
//////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>

template<class T>
class Min
{
public:
 const T& Is (const T &a, const T &b)
 {
  std::cout << "template<class T> class Min::Is ("
      << static_cast<int>(a) << ", "
      << static_cast<int>(b) << ")"
      << std::endl;

  return (a<b) ? a: b;
 }
};

template<>
class Min <char>
{
public:
 const char& Is (const char &a, const char &b)
 {
  std::cout << "template<> class Min<char>::Is ( \'"
      << a << "\', "
      << b << "\' )"
      << std::endl;

  return (a<b) ? a: b;
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 Min<int>  a;
 //Min<>     b; <- error
 Min<char> c;
 //Min<>     d; <- error

 a.Is (1,2);
 //b.Is (3,4);
 c.Is ('a','b');
 //d.Is ('c','d');

 return 0;
}
--------------------------------------------------------------------------------------
template<class T> class Min::Is (1, 2)
template<> class Min<char>::Is ( 'a', b' )





//////////////////////////////////////////////////////////////////////////////////////
//
// 함수 템플릿 특화 (Specialization of function template)
//
//
//////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>

template<class T>
const T& Min (const T &a, const T &b)
{
 std::cout << "template<class T> const T& Min ("
           << static_cast<int>(a) << ", "
     << static_cast<int>(b) << ")"
     << std::endl;

 return (a<b) ? a: b;
}

template<>
const char& Min <char> (const char &a, const char &b)
{
 std::cout << "template<> const char& Min <char> ( \'"
           << a << "\', "
     << b << "\' )"
     << std::endl;

 return (a<b) ? a: b;
}

int _tmain(int argc, _TCHAR* argv[])
{
 Min<int>  ( 1 ,  2 );
 Min<>     ( 3 ,  4 );
 Min<char> ('c', 'd');
 Min<>     ('a', 'b');

 return 0;
}
--------------------------------------------------------------------------------------
template<class T> const T& Min (1, 2)
template<class T> const T& Min (3, 4)
template<> const char& Min <char> ( 'c', d' )
template<> const char& Min <char> ( 'a', b' )

//
// cf>
//   함수 템플릿은 아래와 같이 정의 할 수 있지만
//   template <class T, int X>
//   void foo_func (T a)
//   {
//   }
//   아래와 같이 기본 템플릿 인수는 정의 할 수 없다.
//   template <class T, int X=0>
//   void foo_func (T a)
//   {
//   }
//   기본 템플릿 인수는 클래스 탬플릿에서만 사용이 가능하다.
//
//   또한, 부분특화 템플릿 함수는 사용 될 수 없다.
//





//////////////////////////////////////////////////////////////////////////////////////
//
// 템플릿 클래스 부분특화 (Partial specialization of template class)
//
//
//////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <iostream>

// partial specialization

class partial_specialization
{
public:
 partial_specialization ()
 {
  std::cout << "partial_specialization ()" << std::endl;
 }
 ~partial_specialization ()
 {
  std::cout << "~partial_specialization ()" << std::endl;
 }
};

template <class T1, class T2> class foo
{
public:
 foo ()
 {
  std::cout << "foo ()" << std::endl;
 }

 ~foo ()
 {
  std::cout << "~foo ()" << std::endl;
 }
};

template <class T1> class foo <T1, partial_specialization>
{
public:
 foo ()
 {
  std::cout << "foo() 2" << std::endl;
 }

 ~foo ()
 {
  std::cout << "~foo() 2" << std::endl;
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 foo<int, int>                    a;
 foo<int, partial_specialization> b;

 return 0;
}
--------------------------------------------------------------------------------------
foo ()
foo() 2
~foo() 2
~foo ()

Posted by 셈말짓기 :