template<class T>
class Array
{
public:void mf(); #1
};
template class Array<char>; // explicit instantiation #2
template void Array<int>::mf(); // explicit instantiation #3
void main()
{
Array<double> a; // implicit instantiation
// my question is how to call mf() in #2 (explict declaration)from main()
}
Asked
Active
Viewed 115 times
0
2 Answers
1
The question is a bit unclear, so apologies if I've got the wrong end of the stick.
You call the function on the explicit instantiation just as you would on an implicit instantiation, i.e.
Array<char> c;
c.mf();
For this to work, there must be either a definition of Array<T>::mf()
available when Array<char>
is explicitly instantiated, or a specialisation of Array<char>::mf()
defined. So the above code will work if you either have:
template <typename T> void Array<T>::mf() {cout << "Hello\n";}
template class Array<char>;
or
template <> void Array<char>::mf() {cout << "Hello\n";}

Mike Seymour
- 249,747
- 28
- 448
- 644