How to call Explicit Instantiation Declarations function from main()in template? can you explain with the code given below
template<class T>
class Array
{
public:void mf(); #1
};
template class Array<char>; // explicit instantiation #2
template void Array&l开发者_Go百科t;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()
}
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";}
a.mf();
(If you really didn't know that, Stackoverflow isn't what you need. Look here for help.)
精彩评论