0

Looking for the official name and the linkage type of this type of template specialization where we're specializing a template only to specialize the implementation of a single method within a class template.

template <class T>
class Foo
{
public:
    void f() 
    {
        // default method implementation
    }
};

// What is the default linkage of this function (I'm guessing 
// external based on basic function template specializations), 
// and what am I doing called? (method specialization of a class 
// template?)
template <> 
void Foo<int>::f()
{
     // method implementation for Foo<int>
}
stinky472
  • 6,737
  • 28
  • 27

1 Answers1

2
Foo<int> f1; f1.f(); //Use implementation for Foo<int>, i.e. the specialized one.
Foo<double> f2; f2.f(); //Use the default implementation.

The terminology behind this case is called Explicit Specialization. The compiler will select the "best" matching template it can find. The process to determine the "best" matching template is a little complicated. You may refer to the book "C++ Templates: The Complete Guide" for more details.

shrinker
  • 36
  • 1
  • For the second question about linkage, however, do these explicit specializations get external linkage? Wasn't sure if I need to inline their implementations or put them into a separate compilation unit. – stinky472 Dec 16 '12 at 02:43