template<typename T>
T getMAx(T x, T y)
{
return (x > y) ? x : y;
}
int main()
{
cout << getMax(1, 2) << endl;
cout << getMax(3.14, 1.589) << endl;
cout << getMax(1.0f, 3.4f) << endl;
cout << getMax('a', 'c') << endl;
return 0;
}
// MyArray.h
#pragma once
#inlcude <assert.h> // for assert()
#inlcude <iostream>
template<typename T>
class My Array
{
private:
int m_length;
T *m_data;
public:
MyArray()
{
m_length = 0;
m_data = nullptr;
}
MyArray(int length)
{
m_data = new T [legnth];
m_length = length;
}
~MyArray()
{
reset();
}
void reset()
{
delete[] = m_data;
m_data = nullptr;
m_length = 0;
}
T & operater[](int index)
{
assert(index >= 0 && index < m_length);
return m_data[index]
}
int getLength()
{
return m_length;
}
void print()
{
for(int i = 0; i < m_length; ++i)
cout << m_data[i] << " ";
cout << endl;
}
};
#inlcude "MyArray.h"
int main()
{
MyArray my_array(10);
for(int i = 0; i < my_array.getLength(); ++i)
my_array[i] = i * 10;
my_array.print();
return 0;
}
// MyArray.cpp
#include "MyArray.h"
template<typenmase T>
void MyArray<T>::print()
{
for(int i = 0; i < m_length; ++i)
cout << m_data[i] << " ";
cout << endl;
}
// explictit instantiation 링킹에러 해결
template void MyArray<char>::print();
template void MyArray<char>::print();
template class MyArray<char>;
template class MyArray<double>;
// MyArray.h
#pragma once
#inlcude <assert.h> // for assert()
#inlcude <iostream>
template<typename T, unsigned int T_SIZE> // T_SIZE의 compile time이 결정된다.
//단, 이 경우 explicit instantitation하지 말기.
class My Array
{
private:
int m_length;
T *m_data;
public:
MyArray()
{
//m_length = 0;
m_data = nullptr;
}
.....
};
특수화란? 특정 자료형에 대해 함수 템플릿의 동작을 맞춤 설정하는 것. 특정 자료형에 최적화된 코드를 제공할 때 유용
1) 함수 템플릿 특수화
#include <iostream>
using namespace std;
template<typename T>
T getMax(T x, T y)
{
reutrn (x > y) ? x : y;
}
//specialization
template<> // char가 value로 들어가면 이 친구가 사용된다.
char getMax(char x, char y)
{
cout << "Warning : comparing chars" << endl;
return(x > y) ? x : y;
}
int main()
{
cout << getMax(1, 2) << endl;
return 0;
}
2) 클래스 탬플릿 특수화
#include <iostream>
#include <array>
using namespace std;
template<typename T>
class A
{
public:
void doSomething()
{
cout << typeid(T).name() << endl;
}
void test()
{}
};
template<>
class A<char>
{
public:
void doSomthing();
{
cout << "char type specializatioin" << endl;
}
};
int main()
{
A<int> a_int;
A<double> a_doulbe;
A<char> a_char;
//주의사항
a_int.test();
a.double.test();
// a.char.test(); 실행되지 않는다. 생성자를 각 클래스에 넣어줄 경우 빌드 된다.
a_int.doSomething();
a_double.doSomething();
a_char.doSomething();
return 0;
}
#include <iostream>
using namespace std;
template <class T, int size>
class StaticArray_Base
{
private:
T m_array[size];
public:
T * getArray() { return m_array; }
T & operater[](int index)
{
return m_array[index];
}
void print()
{
for (int count = 0; count < size; ++count)
std::cout << (*this)[count] << ' ';
std::cout << endl;
}
};
template <class T, int size> //상속 받고
class StaticArray : public StaticArray_Base<T, size>
{
};
template <class T, int size> //특수화하기
class StaticArray<char> : public StaticArray_Base<char, size>
{
public:
void print()
{
for (int count = 0; count < size; ++count)
std::cout << (*this)[count];
std::cout << endl;
}
};
template<class T>
class A
{
private:
T m_value;
public:
A(const T & input)
: m_value(input)
{
}
void print()
{
cout << m_value << endl;
}
};
template<class T>
class A<T*>
{
private:
T* m_value;
public:
A(const * input)
: m_value(input)
{
}
void print()
{
cout << *m_value << endl;
}
};
int main()
{
A<int> a_int(123);
a.int.print();
int temp = 456;
A<int*> a_int_ptr(&temp);
a_int_ptr.print();
double temp_d = 3.141592;
A<double*> a_double_ptr(&temp_d);
a_double_ptr.print();
return 0;
}
template<class T>
class A
{
private:
T m_value;
public:
A(const T & input)
: m_value(input)
{
}
template<typename TT>
void doSomething(const TT & input) // const TT & input이 중요
{
cout << typeid(T).name() << " " << typeid(TT).name() << endl;
}
void print()
{
cout << m_value << endl;
}
};
int main()
{
A<int> a_int(123);
a.int.print();
a_int.doSomehing(123.4);
return 0;
}