.h
template < class type-param [ , class type-param ]... > class class-name { ... }
typename
works for class
too.
template <typename EType> struct stack { void push(const EType & e) { stk.push_back(e) } std::vector<EType> stk }
typename
keyword may be necessary to indicate
type specs.
template <class ContainerType>
struct C {
typename ContainerType::value_type front() {
return c.front()
}
ContainerType c
}
stack<int> istk std::map<std::string, int> dictionary
>>
problem.
template <typename EType> struct stack { void push(const EType &) } void stack:: push(const EType & e) { ... }
stack
? What is stack
? And what is EType
?
void stack<EType>::
push(const EType & e) { ... }
EType
?
template < typename EType >
void stack<EType>::
push(const EType & e) { ... }
.h
.h
files.
.h
files is frowned upon.
.h
files.
.h
file.
.cc
file.
pragma
s.
export
keyword.
export
.
template < class A, class B > struct C1 { template < typename X, typename Y > A f(X x, Y y) { B b; } }; int main() { C1<int, std::string> c; int i = c.f(1, 'a'); int j = c.f("hello", 1.0); }
struct blob template < class T > blob(const T & t) cout << "blob::blob(const T &)" template < class T> blob & operator = (const T & t) cout << "blob::operator =" return *this;
$ cat t.cc int main() { blob b1; blob b2 = b1; b1 = b2; } $ g++ -o t -ansi -pedantic -Wall t.cc $ ./t $
Nothing gets printed. Why?
blob
has no explicit copy constructor and no assignment operator.
$ cat t.cc struct frog { } int main() frog f blob b1 = f b1 = f $ g++ -o t -ansi -pedantic -Wall t.cc $ ./t blob::blob(const T &) blob::operator = $
struct blob template < class T > blob(const T & t) cout << "blob::blob(const T &)" blob(const blob & t) cout << "blob::blob(blob)" template < class T > blob & operator = (const T & t) cout << "blob::operator == (T)" return *this blob & operator = (const blob & t) { cout << "blob::operator == (blob)" return *this
$ cat t.cc int main() blob b1 blob b2 = b1 b1 = b2 $ g++ -o t -ansi -pedantic -Wall t.cc $ ./t blob::blob(const blob &) blob::operator == (const blob &) $
template < typename T, class C = std::deque<T> > class Stack template < typename T2, class C2> Stack<T, C> & operator = (const Stack<T2, C2> &); template < typename T, class C> template < typename T2, class C2> Stack<T, C> & Stack<T, C>:: operator = (const Stack<T2, C2> & rhs) if ((void *) this == (void *) &rhs) // self assignment (not reached)
template < typename EType, int size > class stack void push(const EType & e) { if (top >= size) { ... } } private: EType arr[size]
The value template parameter is treated as a named constant.
int
s, pointers, and
references.
int
s includes characters, enums, and booleans.
template < typename EType, int size = 100 > class stack { ... } int main() stack<int, 200> istk stack<std::string> sstk
$ cat t.cc int main() stk<int, 10> s10 stk<int, 20> s20 s10 = s20 $ g++ -c -ansi -pedantic -Wall t.cc t.cc: In function int main(): no match for stk<int,10>& = stk<int,20>& candidates are: stk<int,10> & stk<int,10>::operator=(const stk<int,10> &) $
template < typename EType = int, int size = 100 > class stack { ... } int main() stack<blob, 200> bstk stack<std::string> sstk stack<> istk
Note that funky syntax.
This page last modified on 25 March 2004.