template<typename T> scope?

if I define a

template<typename T> in header file a.h, then any file with #include "a.h" will know T is a template typename?
No. 'The 'T' is just a placeholder for the function / entity it is associated with. Also, remember that template functions and entities have to be implemented in the header file as well as declared.

Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// a.h
#ifndef A_H
#define A_H

// A template function
template <typename T> // notice the lack of a semicolon here - it is part of the function
T max(const T& rhs, const T& lhs) {
    return (rhs > lhs ? rhs : lhs);
}

T val; // doesn't work: gives you an error

#endif


// a.cpp
#include "a.h"
#include <iostream>
...

int x = 6, y = 10;

// explicitly specifying 'T' here
std::cout << ::max<int>(x, y) << std::endl;

// can't do this
std::cout << ::max<T>(x, y) << std::endl;
Topic archived. No new replies allowed.