0
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()
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Suri
  • 3,287
  • 9
  • 45
  • 75

2 Answers2

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
0
a.mf();

(If you really didn't know that, Stackoverflow isn't what you need. Look here for help.)

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445