简体   繁体   中英

explicit instantiation after specialization

#include <iostream>
using namespace std;

template <typename>
  class Test
{
  void fun() { cout << "test" << endl; } 
  void bar() { cout << "bar"; }
};


template<>
  class Test<int>
{
  void fun(){}
};

template void Test<int>::fun();

I got an error:

error: template-id 'fun<>' for 'void Test::fun()' does not match any template declaration

But why?

I know it work if add template for fun in Test eg

template<>
  class Test<int>
{
  template <typename>
  void fun(){}
};

template void Test<int>::fun<bool>();

For function template

template<class T> void sort(Array<T>& v) { /*...*/ } // primary template

template<>  //explicit specialization of sort(Array<String>)
void sort<String>(Array<String>& v); // after implicit instantiation

template
void sort(Array<String>& v);// no matter before/after  void f(Array<String>& v) , it both works

void f(Array<String>& v) {
    sort(v); // implicitly instantiates sort(Array<String>&), 
}            // using the primary template for sort()

An explicit specialisation (that is, not a partial specialisation) is no longer a template. That means all of its members really exist (as if they were instantiated), so you cannot (and need not) instantiate them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM